qdev.c 30.8 KB
Newer Older
P
Paul Brook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  Dynamic device configuration and creation.
 *
 *  Copyright (c) 2009 CodeSourcery
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
P
Paul Brook 已提交
18 19 20 21 22 23 24 25 26 27
 */

/* The theory here is that it should be possible to create a machine without
   knowledge of specific devices.  Historically board init routines have
   passed a bunch of arguments to each device, requiring the board know
   exactly which device it is dealing with.  This file provides an abstract
   API for device configuration and initialization.  Devices will generally
   inherit from a particular bus (e.g. PCI or I2C) rather than
   this API directly.  */

28
#include "hw/qdev.h"
29
#include "hw/fw-path-provider.h"
30
#include "sysemu/sysemu.h"
31
#include "qapi/error.h"
32
#include "qapi/qmp/qerror.h"
33
#include "qapi/visitor.h"
M
Michael S. Tsirkin 已提交
34
#include "qapi/qmp/qjson.h"
35
#include "hw/hotplug.h"
36
#include "hw/boards.h"
W
Wenchao Xia 已提交
37
#include "qapi-event.h"
P
Paul Brook 已提交
38

39
int qdev_hotplug = 0;
40 41
static bool qdev_hot_added = false;
static bool qdev_hot_removed = false;
42

A
Anthony Liguori 已提交
43 44
const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
{
A
Anthony Liguori 已提交
45 46
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
    return dc->vmsd;
A
Anthony Liguori 已提交
47 48 49 50
}

const char *qdev_fw_name(DeviceState *dev)
{
A
Anthony Liguori 已提交
51
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
A
Anthony Liguori 已提交
52

A
Anthony Liguori 已提交
53 54
    if (dc->fw_name) {
        return dc->fw_name;
A
Anthony Liguori 已提交
55 56 57 58 59
    }

    return object_get_typename(OBJECT(dev));
}

60 61 62
static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
                                     Error **errp);

A
Anthony Liguori 已提交
63
static void bus_remove_child(BusState *bus, DeviceState *child)
64
{
A
Anthony Liguori 已提交
65 66 67 68 69 70 71 72
    BusChild *kid;

    QTAILQ_FOREACH(kid, &bus->children, sibling) {
        if (kid->child == child) {
            char name[32];

            snprintf(name, sizeof(name), "child[%d]", kid->index);
            QTAILQ_REMOVE(&bus->children, kid, sibling);
73 74

            /* This gives back ownership of kid->child back to us.  */
A
Anthony Liguori 已提交
75
            object_property_del(OBJECT(bus), name, NULL);
76
            object_unref(OBJECT(kid->child));
A
Anthony Liguori 已提交
77 78 79 80 81 82 83 84 85 86
            g_free(kid);
            return;
        }
    }
}

static void bus_add_child(BusState *bus, DeviceState *child)
{
    char name[32];
    BusChild *kid = g_malloc0(sizeof(*kid));
87 88 89 90

    if (qdev_hotplug) {
        assert(bus->allow_hotplug);
    }
91

A
Anthony Liguori 已提交
92 93
    kid->index = bus->max_index++;
    kid->child = child;
94
    object_ref(OBJECT(kid->child));
95

A
Anthony Liguori 已提交
96 97
    QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);

98
    /* This transfers ownership of kid->child to the property.  */
A
Anthony Liguori 已提交
99 100 101
    snprintf(name, sizeof(name), "child[%d]", kid->index);
    object_property_add_link(OBJECT(bus), name,
                             object_get_typename(OBJECT(child)),
102 103 104 105
                             (Object **)&kid->child,
                             NULL, /* read-only property */
                             0, /* return ownership on prop deletion */
                             NULL);
A
Anthony Liguori 已提交
106 107 108 109
}

void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
{
110
    dev->parent_bus = bus;
111
    object_ref(OBJECT(bus));
A
Anthony Liguori 已提交
112
    bus_add_child(bus, dev);
113 114
}

P
Paul Brook 已提交
115 116 117
/* Create a new device.  This only initializes the device state structure
   and allows properties to be set.  qdev_init should be called to
   initialize the actual device emulation.  */
P
Paul Brook 已提交
118
DeviceState *qdev_create(BusState *bus, const char *name)
119 120 121 122 123
{
    DeviceState *dev;

    dev = qdev_try_create(bus, name);
    if (!dev) {
124
        if (bus) {
125
            error_report("Unknown device '%s' for bus '%s'", name,
126
                         object_get_typename(OBJECT(bus)));
127
        } else {
128
            error_report("Unknown device '%s' for default sysbus", name);
129
        }
L
liguang 已提交
130
        abort();
131 132 133 134 135
    }

    return dev;
}

136
DeviceState *qdev_try_create(BusState *bus, const char *type)
P
Paul Brook 已提交
137
{
138 139
    DeviceState *dev;

140
    if (object_class_by_name(type) == NULL) {
141 142
        return NULL;
    }
143
    dev = DEVICE(object_new(type));
144 145 146 147
    if (!dev) {
        return NULL;
    }

148
    if (!bus) {
149
        bus = sysbus_get_default();
150 151
    }

152
    qdev_set_parent_bus(dev, bus);
153
    object_unref(OBJECT(dev));
154
    return dev;
P
Paul Brook 已提交
155 156 157 158
}

/* Initialize a device.  Device properties should be set before calling
   this function.  IRQs and MMIO regions should be connected/mapped after
159 160 161
   calling this function.
   On failure, destroy the device and return negative value.
   Return 0 on success.  */
162
int qdev_init(DeviceState *dev)
P
Paul Brook 已提交
163
{
164
    Error *local_err = NULL;
G
Gerd Hoffmann 已提交
165

166
    assert(!dev->realized);
A
Anthony Liguori 已提交
167

168 169
    object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
    if (local_err != NULL) {
170
        qerror_report_err(local_err);
171
        error_free(local_err);
172
        object_unparent(OBJECT(dev));
173
        return -1;
174
    }
175 176
    return 0;
}
177

178
static void device_realize(DeviceState *dev, Error **errp)
179 180
{
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
181

182 183 184
    if (dc->init) {
        int rc = dc->init(dev);
        if (rc < 0) {
185
            error_setg(errp, "Device initialization failed.");
186 187
            return;
        }
J
Jan Kiszka 已提交
188
    }
P
Paul Brook 已提交
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203
static void device_unrealize(DeviceState *dev, Error **errp)
{
    DeviceClass *dc = DEVICE_GET_CLASS(dev);

    if (dc->exit) {
        int rc = dc->exit(dev);
        if (rc < 0) {
            error_setg(errp, "Device exit failed.");
            return;
        }
    }
}

J
Jan Kiszka 已提交
204 205 206
void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                 int required_for_version)
{
207
    assert(!dev->realized);
J
Jan Kiszka 已提交
208 209 210 211
    dev->instance_id_alias = alias_id;
    dev->alias_required_for_version = required_for_version;
}

212
void qdev_unplug(DeviceState *dev, Error **errp)
213
{
A
Anthony Liguori 已提交
214 215
    DeviceClass *dc = DEVICE_GET_CLASS(dev);

216
    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
217 218
        error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
        return;
219
    }
220

221 222 223 224 225 226
    if (!dc->hotpluggable) {
        error_set(errp, QERR_DEVICE_NO_HOTPLUG,
                  object_get_typename(OBJECT(dev)));
        return;
    }

227 228
    qdev_hot_removed = true;

229 230 231 232 233 234 235
    if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
        hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
    } else {
        assert(dc->unplug != NULL);
        if (dc->unplug(dev) < 0) { /* legacy handler */
            error_set(errp, QERR_UNDEFINED_ERROR);
        }
236
    }
237 238
}

239 240
static int qdev_reset_one(DeviceState *dev, void *opaque)
{
241
    device_reset(dev);
242 243 244 245

    return 0;
}

246 247
static int qbus_reset_one(BusState *bus, void *opaque)
{
248 249
    BusClass *bc = BUS_GET_CLASS(bus);
    if (bc->reset) {
P
Paolo Bonzini 已提交
250
        bc->reset(bus);
251 252 253 254
    }
    return 0;
}

255 256
void qdev_reset_all(DeviceState *dev)
{
P
Paolo Bonzini 已提交
257
    qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
258 259
}

P
Paolo Bonzini 已提交
260 261
void qbus_reset_all(BusState *bus)
{
P
Paolo Bonzini 已提交
262
    qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
P
Paolo Bonzini 已提交
263 264
}

265 266 267
void qbus_reset_all_fn(void *opaque)
{
    BusState *bus = opaque;
P
Paolo Bonzini 已提交
268
    qbus_reset_all(bus);
269 270
}

271 272 273 274
/* can be used as ->unplug() callback for the simple cases */
int qdev_simple_unplug_cb(DeviceState *dev)
{
    /* just zap it */
275
    object_unparent(OBJECT(dev));
276 277 278
    return 0;
}

279 280

/* Like qdev_init(), but terminate program via error_report() instead of
M
Markus Armbruster 已提交
281 282 283 284 285 286 287 288
   returning an error value.  This is okay during machine creation.
   Don't use for hotplug, because there callers need to recover from
   failure.  Exception: if you know the device's init() callback can't
   fail, then qdev_init_nofail() can't fail either, and is therefore
   usable even then.  But relying on the device implementation that
   way is somewhat unclean, and best avoided.  */
void qdev_init_nofail(DeviceState *dev)
{
289 290
    const char *typename = object_get_typename(OBJECT(dev));

291
    if (qdev_init(dev) < 0) {
292
        error_report("Initialization of device %s failed", typename);
293 294
        exit(1);
    }
M
Markus Armbruster 已提交
295 296
}

297 298 299 300 301 302 303 304 305
void qdev_machine_creation_done(void)
{
    /*
     * ok, initial machine setup is done, starting from now we can
     * only create hotpluggable devices
     */
    qdev_hotplug = 1;
}

306 307 308 309 310
bool qdev_machine_modified(void)
{
    return qdev_hot_added || qdev_hot_removed;
}

P
Paul Brook 已提交
311
BusState *qdev_get_parent_bus(DeviceState *dev)
P
Paul Brook 已提交
312
{
P
Paul Brook 已提交
313
    return dev->parent_bus;
P
Paul Brook 已提交
314 315
}

P
Peter Crosthwaite 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
                                               const char *name)
{
    NamedGPIOList *ngl;

    QLIST_FOREACH(ngl, &dev->gpios, node) {
        /* NULL is a valid and matchable name, otherwise do a normal
         * strcmp match.
         */
        if ((!ngl->name && !name) ||
                (name && ngl->name && strcmp(name, ngl->name) == 0)) {
            return ngl;
        }
    }

    ngl = g_malloc0(sizeof(*ngl));
    ngl->name = g_strdup(name);
    QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
    return ngl;
}

void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
                             const char *name, int n)
{
    NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);

342
    assert(gpio_list->num_out == 0 || !name);
P
Peter Crosthwaite 已提交
343 344 345 346 347
    gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
                                     dev, n);
    gpio_list->num_in += n;
}

P
Paul Brook 已提交
348 349
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
{
P
Peter Crosthwaite 已提交
350 351 352 353 354 355 356 357
    qdev_init_gpio_in_named(dev, handler, NULL, n);
}

void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
                              const char *name, int n)
{
    NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);

358
    assert(gpio_list->num_in == 0 || !name);
P
Peter Crosthwaite 已提交
359 360 361
    assert(gpio_list->num_out == 0);
    gpio_list->num_out = n;
    gpio_list->out = pins;
P
Paul Brook 已提交
362 363 364 365
}

void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
{
P
Peter Crosthwaite 已提交
366 367 368 369 370 371 372 373 374
    qdev_init_gpio_out_named(dev, pins, NULL, n);
}

qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
{
    NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);

    assert(n >= 0 && n < gpio_list->num_in);
    return gpio_list->in[n];
P
Paul Brook 已提交
375 376 377 378
}

qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
{
P
Peter Crosthwaite 已提交
379 380 381 382 383 384 385 386 387 388
    return qdev_get_gpio_in_named(dev, NULL, n);
}

void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
                                 qemu_irq pin)
{
    NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);

    assert(n >= 0 && n < gpio_list->num_out);
    gpio_list->out[n] = pin;
P
Paul Brook 已提交
389 390 391 392
}

void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
{
P
Peter Crosthwaite 已提交
393
    qdev_connect_gpio_out_named(dev, NULL, n, pin);
P
Paul Brook 已提交
394 395
}

P
Paul Brook 已提交
396
BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
P
Paul Brook 已提交
397
{
P
Paul Brook 已提交
398
    BusState *bus;
P
Paul Brook 已提交
399

B
Blue Swirl 已提交
400
    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
P
Paul Brook 已提交
401
        if (strcmp(name, bus->name) == 0) {
P
Paul Brook 已提交
402
            return bus;
P
Paul Brook 已提交
403 404 405 406 407
        }
    }
    return NULL;
}

408 409 410 411
int qbus_walk_children(BusState *bus,
                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
                       void *opaque)
412
{
A
Anthony Liguori 已提交
413
    BusChild *kid;
414 415
    int err;

416 417
    if (pre_busfn) {
        err = pre_busfn(bus, opaque);
418 419 420 421 422
        if (err) {
            return err;
        }
    }

A
Anthony Liguori 已提交
423
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
424 425 426
        err = qdev_walk_children(kid->child,
                                 pre_devfn, pre_busfn,
                                 post_devfn, post_busfn, opaque);
427 428 429 430 431
        if (err < 0) {
            return err;
        }
    }

432 433 434 435 436 437 438
    if (post_busfn) {
        err = post_busfn(bus, opaque);
        if (err) {
            return err;
        }
    }

439 440 441
    return 0;
}

442 443 444 445
int qdev_walk_children(DeviceState *dev,
                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
                       void *opaque)
446 447 448 449
{
    BusState *bus;
    int err;

450 451
    if (pre_devfn) {
        err = pre_devfn(dev, opaque);
452 453 454 455 456 457
        if (err) {
            return err;
        }
    }

    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
458 459
        err = qbus_walk_children(bus, pre_devfn, pre_busfn,
                                 post_devfn, post_busfn, opaque);
460 461 462 463 464
        if (err < 0) {
            return err;
        }
    }

465 466 467 468 469 470 471
    if (post_devfn) {
        err = post_devfn(dev, opaque);
        if (err) {
            return err;
        }
    }

472 473 474
    return 0;
}

475
DeviceState *qdev_find_recursive(BusState *bus, const char *id)
476
{
A
Anthony Liguori 已提交
477 478
    BusChild *kid;
    DeviceState *ret;
479 480
    BusState *child;

A
Anthony Liguori 已提交
481 482 483 484
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
        DeviceState *dev = kid->child;

        if (dev->id && strcmp(dev->id, id) == 0) {
485
            return dev;
A
Anthony Liguori 已提交
486 487
        }

488 489 490 491 492 493 494 495 496 497
        QLIST_FOREACH(child, &dev->child_bus, sibling) {
            ret = qdev_find_recursive(child, id);
            if (ret) {
                return ret;
            }
        }
    }
    return NULL;
}

498
static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
P
Paul Brook 已提交
499
{
500
    const char *typename = object_get_typename(OBJECT(bus));
501
    BusClass *bc;
G
Gerd Hoffmann 已提交
502
    char *buf;
503
    int i, len, bus_id;
P
Paul Brook 已提交
504

505 506 507 508
    bus->parent = parent;

    if (name) {
        bus->name = g_strdup(name);
509
    } else if (bus->parent && bus->parent->id) {
510 511 512
        /* parent device has id -> use it plus parent-bus-id for bus name */
        bus_id = bus->parent->num_child_bus;

513
        len = strlen(bus->parent->id) + 16;
514
        buf = g_malloc(len);
515
        snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
G
Gerd Hoffmann 已提交
516 517
        bus->name = buf;
    } else {
518 519 520 521
        /* no id -> use lowercase bus type plus global bus-id for bus name */
        bc = BUS_GET_CLASS(bus);
        bus_id = bc->automatic_ids++;

522
        len = strlen(typename) + 16;
523
        buf = g_malloc(len);
524 525
        len = snprintf(buf, len, "%s.%d", typename, bus_id);
        for (i = 0; i < len; i++) {
C
Christoph Egger 已提交
526
            buf[i] = qemu_tolower(buf[i]);
527
        }
G
Gerd Hoffmann 已提交
528 529 530
        bus->name = buf;
    }

531 532 533 534
    if (bus->parent) {
        QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
        bus->parent->num_child_bus++;
        object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
535
        object_unref(OBJECT(bus));
536
    } else if (bus != sysbus_get_default()) {
537 538 539
        /* TODO: once all bus devices are qdevified,
           only reset handler for main_system_bus should be registered here. */
        qemu_register_reset(qbus_reset_all_fn, bus);
P
Paul Brook 已提交
540
    }
541 542
}

543 544 545 546 547 548 549
static void bus_unparent(Object *obj)
{
    BusState *bus = BUS(obj);
    BusChild *kid;

    while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
        DeviceState *dev = kid->child;
550
        object_unparent(OBJECT(dev));
551 552 553 554 555 556 557 558 559 560 561
    }
    if (bus->parent) {
        QLIST_REMOVE(bus, sibling);
        bus->parent->num_child_bus--;
        bus->parent = NULL;
    } else {
        assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
        qemu_unregister_reset(qbus_reset_all_fn, bus);
    }
}

562
static bool bus_get_realized(Object *obj, Error **errp)
563 564 565 566 567 568
{
    BusState *bus = BUS(obj);

    return bus->realized;
}

569
static void bus_set_realized(Object *obj, bool value, Error **errp)
570 571 572
{
    BusState *bus = BUS(obj);
    BusClass *bc = BUS_GET_CLASS(bus);
573
    BusChild *kid;
574 575 576 577 578 579
    Error *local_err = NULL;

    if (value && !bus->realized) {
        if (bc->realize) {
            bc->realize(bus, &local_err);
        }
580 581

        /* TODO: recursive realization */
582
    } else if (!value && bus->realized) {
583 584 585 586 587 588 589 590 591
        QTAILQ_FOREACH(kid, &bus->children, sibling) {
            DeviceState *dev = kid->child;
            object_property_set_bool(OBJECT(dev), false, "realized",
                                     &local_err);
            if (local_err != NULL) {
                break;
            }
        }
        if (bc->unrealize && local_err == NULL) {
592 593 594 595
            bc->unrealize(bus, &local_err);
        }
    }

596 597 598 599
    if (local_err != NULL) {
        error_propagate(errp, local_err);
        return;
    }
600

601
    bus->realized = value;
602 603
}

604
void qbus_create_inplace(void *bus, size_t size, const char *typename,
605
                         DeviceState *parent, const char *name)
606
{
607
    object_initialize(bus, size, typename);
608
    qbus_realize(bus, parent, name);
P
Paul Brook 已提交
609
}
610

611
BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
I
Isaku Yamahata 已提交
612
{
613 614
    BusState *bus;

615
    bus = BUS(object_new(typename));
616
    qbus_realize(bus, parent, name);
617

P
Paul Brook 已提交
618
    return bus;
I
Isaku Yamahata 已提交
619 620
}

621 622 623 624 625 626
static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
{
    BusClass *bc = BUS_GET_CLASS(bus);

    if (bc->get_fw_dev_path) {
        return bc->get_fw_dev_path(dev);
G
Gerd Hoffmann 已提交
627
    }
628 629

    return NULL;
G
Gerd Hoffmann 已提交
630 631
}

632 633 634 635 636 637 638 639 640 641 642 643
static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
{
    Object *obj = OBJECT(dev);
    char *d = NULL;

    while (!d && obj->parent) {
        obj = obj->parent;
        d = fw_path_provider_try_get_dev_path(obj, bus, dev);
    }
    return d;
}

644 645 646 647 648 649 650
static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
{
    int l = 0;

    if (dev && dev->parent_bus) {
        char *d;
        l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
651 652 653 654
        d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
        if (!d) {
            d = bus_get_fw_dev_path(dev->parent_bus, dev);
        }
655
        if (d) {
656
            l += snprintf(p + l, size - l, "%s", d);
657
            g_free(d);
658
        } else {
659
            return l;
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
        }
    }
    l += snprintf(p + l , size - l, "/");

    return l;
}

char* qdev_get_fw_dev_path(DeviceState *dev)
{
    char path[128];
    int l;

    l = qdev_get_fw_dev_path_helper(dev, path, 128);

    path[l-1] = '\0';

676
    return g_strdup(path);
677
}
678

679
char *qdev_get_dev_path(DeviceState *dev)
680
{
681
    BusClass *bc;
682 683 684 685 686

    if (!dev || !dev->parent_bus) {
        return NULL;
    }

687 688 689
    bc = BUS_GET_CLASS(dev->parent_bus);
    if (bc->get_dev_path) {
        return bc->get_dev_path(dev);
690 691 692
    }

    return NULL;
693
}
694 695 696 697 698

/**
 * Legacy property handling
 */

699
static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
700 701
                                     const char *name, Error **errp)
{
702
    DeviceState *dev = DEVICE(obj);
703 704
    Property *prop = opaque;

705 706
    char buffer[1024];
    char *ptr = buffer;
707

708 709
    prop->info->print(dev, prop, buffer, sizeof(buffer));
    visit_type_str(v, &ptr, name, errp);
710 711 712 713 714 715
}

/**
 * @qdev_add_legacy_property - adds a legacy property
 *
 * Do not use this is new code!  Properties added through this interface will
716
 * be given names and types in the "legacy" namespace.
717
 *
718 719
 * Legacy properties are string versions of other OOM properties.  The format
 * of the string depends on the property type.
720
 */
721 722
static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
                                     Error **errp)
723
{
724
    gchar *name;
725

726
    /* Register pointer properties as legacy properties */
727
    if (!prop->info->print && prop->info->get) {
728 729
        return;
    }
730

731
    name = g_strdup_printf("legacy-%s", prop->name);
732
    object_property_add(OBJECT(dev), name, "str",
733
                        prop->info->print ? qdev_get_legacy_property : prop->info->get,
734
                        NULL,
735 736
                        NULL,
                        prop, errp);
737

738 739 740 741 742 743 744 745 746 747 748 749
    g_free(name);
}

/**
 * @qdev_property_add_static - add a @Property to a device.
 *
 * Static properties access data in a struct.  The actual type of the
 * property and the field depends on the property type.
 */
void qdev_property_add_static(DeviceState *dev, Property *prop,
                              Error **errp)
{
750 751 752
    Error *local_err = NULL;
    Object *obj = OBJECT(dev);

753 754 755 756 757 758 759 760 761
    /*
     * TODO qdev_prop_ptr does not have getters or setters.  It must
     * go now that it can be replaced with links.  The test should be
     * removed along with it: all static properties are read/write.
     */
    if (!prop->info->get && !prop->info->set) {
        return;
    }

762
    object_property_add(obj, prop->name, prop->info->name,
763
                        prop->info->get, prop->info->set,
P
Paolo Bonzini 已提交
764
                        prop->info->release,
765 766 767 768 769 770 771 772 773 774 775
                        prop, &local_err);

    if (local_err) {
        error_propagate(errp, local_err);
        return;
    }
    if (prop->qtype == QTYPE_NONE) {
        return;
    }

    if (prop->qtype == QTYPE_QBOOL) {
776
        object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
777 778
    } else if (prop->info->enum_table) {
        object_property_set_str(obj, prop->info->enum_table[prop->defval],
779
                                prop->name, &error_abort);
780
    } else if (prop->qtype == QTYPE_QINT) {
781
        object_property_set_int(obj, prop->defval, prop->name, &error_abort);
782
    }
A
Anthony Liguori 已提交
783
}
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
/* @qdev_alias_all_properties - Add alias properties to the source object for
 * all qdev properties on the target DeviceState.
 */
void qdev_alias_all_properties(DeviceState *target, Object *source)
{
    ObjectClass *class;
    Property *prop;

    class = object_get_class(OBJECT(target));
    do {
        DeviceClass *dc = DEVICE_CLASS(class);

        for (prop = dc->props; prop && prop->name; prop++) {
            object_property_add_alias(source, prop->name,
                                      OBJECT(target), prop->name,
                                      &error_abort);
        }
        class = object_class_get_parent(class);
    } while (class != object_class_by_name(TYPE_DEVICE));
}

806
static bool device_get_realized(Object *obj, Error **errp)
807 808 809 810 811
{
    DeviceState *dev = DEVICE(obj);
    return dev->realized;
}

812
static void device_set_realized(Object *obj, bool value, Error **errp)
813 814 815
{
    DeviceState *dev = DEVICE(obj);
    DeviceClass *dc = DEVICE_GET_CLASS(dev);
816
    BusState *bus;
817 818
    Error *local_err = NULL;

819
    if (dev->hotplugged && !dc->hotpluggable) {
820
        error_set(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
821 822 823
        return;
    }

824
    if (value && !dev->realized) {
825
        if (!obj->parent) {
826 827 828 829 830
            static int unattached_count;
            gchar *name = g_strdup_printf("device[%d]", unattached_count++);

            object_property_add_child(container_get(qdev_get_machine(),
                                                    "/unattached"),
831
                                      name, obj, &error_abort);
832 833 834
            g_free(name);
        }

835 836 837 838
        if (dc->realize) {
            dc->realize(dev, &local_err);
        }

839 840 841 842 843
        if (local_err != NULL) {
            goto fail;
        }

        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
844 845
            hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                 dev, &local_err);
846
        } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
847 848 849 850 851 852 853 854 855 856
            HotplugHandler *hotplug_ctrl;
            MachineState *machine = MACHINE(qdev_get_machine());
            MachineClass *mc = MACHINE_GET_CLASS(machine);

            if (mc->get_hotplug_handler) {
                hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
                if (hotplug_ctrl) {
                    hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
                }
            }
857 858
        }

859 860 861 862 863
        if (local_err != NULL) {
            goto post_realize_fail;
        }

        if (qdev_get_vmsd(dev)) {
864 865 866 867
            vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
                                           dev->instance_id_alias,
                                           dev->alias_required_for_version);
        }
868 869 870

        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
            object_property_set_bool(OBJECT(bus), true, "realized",
871
                                         &local_err);
872 873
            if (local_err != NULL) {
                goto child_realize_fail;
874 875
            }
        }
876
        if (dev->hotplugged) {
877 878
            device_reset(dev);
        }
879
        dev->pending_deleted_event = false;
880
    } else if (!value && dev->realized) {
881
        Error **local_errp = NULL;
882
        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
883
            local_errp = local_err ? NULL : &local_err;
884
            object_property_set_bool(OBJECT(bus), false, "realized",
885
                                     local_errp);
886
        }
887
        if (qdev_get_vmsd(dev)) {
888 889
            vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
        }
890 891 892
        if (dc->unrealize) {
            local_errp = local_err ? NULL : &local_err;
            dc->unrealize(dev, local_errp);
893
        }
894
        dev->pending_deleted_event = true;
895 896 897
    }

    if (local_err != NULL) {
898
        goto fail;
899 900 901
    }

    dev->realized = value;
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    return;

child_realize_fail:
    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
        object_property_set_bool(OBJECT(bus), false, "realized",
                                 NULL);
    }

    if (qdev_get_vmsd(dev)) {
        vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
    }

post_realize_fail:
    if (dc->unrealize) {
        dc->unrealize(dev, NULL);
    }

fail:
    error_propagate(errp, local_err);
    return;
922 923
}

924
static bool device_get_hotpluggable(Object *obj, Error **errp)
925 926 927 928
{
    DeviceClass *dc = DEVICE_GET_CLASS(obj);
    DeviceState *dev = DEVICE(obj);

929 930
    return dc->hotpluggable && (dev->parent_bus == NULL ||
                                dev->parent_bus->allow_hotplug);
931 932
}

933 934 935 936 937 938 939 940 941 942 943 944 945 946
static bool device_get_hotplugged(Object *obj, Error **err)
{
    DeviceState *dev = DEVICE(obj);

    return dev->hotplugged;
}

static void device_set_hotplugged(Object *obj, bool value, Error **err)
{
    DeviceState *dev = DEVICE(obj);

    dev->hotplugged = value;
}

947 948 949
static void device_initfn(Object *obj)
{
    DeviceState *dev = DEVICE(obj);
950
    ObjectClass *class;
951 952 953 954 955 956 957 958
    Property *prop;

    if (qdev_hotplug) {
        dev->hotplugged = 1;
        qdev_hot_added = true;
    }

    dev->instance_id_alias = -1;
959
    dev->realized = false;
960

961 962
    object_property_add_bool(obj, "realized",
                             device_get_realized, device_set_realized, NULL);
963 964
    object_property_add_bool(obj, "hotpluggable",
                             device_get_hotpluggable, NULL, NULL);
965 966 967
    object_property_add_bool(obj, "hotplugged",
                             device_get_hotplugged, device_set_hotplugged,
                             &error_abort);
968

969 970 971
    class = object_get_class(OBJECT(dev));
    do {
        for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
972 973
            qdev_property_add_legacy(dev, prop, &error_abort);
            qdev_property_add_static(dev, prop, &error_abort);
974 975 976
        }
        class = object_class_get_parent(class);
    } while (class != object_class_by_name(TYPE_DEVICE));
977

978
    object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
979
                             (Object **)&dev->parent_bus, NULL, 0,
980
                             &error_abort);
P
Peter Crosthwaite 已提交
981
    QLIST_INIT(&dev->gpios);
982 983
}

984 985
static void device_post_init(Object *obj)
{
986 987 988 989 990 991 992
    Error *err = NULL;
    qdev_prop_set_globals(DEVICE(obj), &err);
    if (err) {
        qerror_report_err(err);
        error_free(err);
        exit(EXIT_FAILURE);
    }
993 994
}

995 996 997
/* Unlink device from bus and free the structure.  */
static void device_finalize(Object *obj)
{
P
Peter Crosthwaite 已提交
998 999
    NamedGPIOList *ngl, *next;

1000
    DeviceState *dev = DEVICE(obj);
1001 1002
    if (dev->opts) {
        qemu_opts_del(dev->opts);
1003
    }
P
Peter Crosthwaite 已提交
1004 1005 1006

    QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
        QLIST_REMOVE(ngl, node);
1007
        qemu_free_irqs(ngl->in, ngl->num_in);
P
Peter Crosthwaite 已提交
1008 1009 1010 1011 1012 1013
        g_free(ngl->name);
        g_free(ngl);
        /* ngl->out irqs are owned by the other end and should not be freed
         * here
         */
    }
1014 1015
}

1016 1017 1018 1019 1020 1021 1022 1023
static void device_class_base_init(ObjectClass *class, void *data)
{
    DeviceClass *klass = DEVICE_CLASS(class);

    /* We explicitly look up properties in the superclasses,
     * so do not propagate them to the subclasses.
     */
    klass->props = NULL;
1024 1025
}

1026
static void device_unparent(Object *obj)
1027 1028
{
    DeviceState *dev = DEVICE(obj);
1029
    BusState *bus;
1030

1031 1032 1033
    if (dev->realized) {
        object_property_set_bool(obj, false, "realized", NULL);
    }
1034 1035
    while (dev->num_child_bus) {
        bus = QLIST_FIRST(&dev->child_bus);
1036
        object_unparent(OBJECT(bus));
1037 1038
    }
    if (dev->parent_bus) {
1039
        bus_remove_child(dev->parent_bus, dev);
1040 1041
        object_unref(OBJECT(dev->parent_bus));
        dev->parent_bus = NULL;
1042
    }
M
Michael S. Tsirkin 已提交
1043

1044
    /* Only send event if the device had been completely realized */
1045
    if (dev->pending_deleted_event) {
1046 1047
        gchar *path = object_get_canonical_path(OBJECT(dev));

W
Wenchao Xia 已提交
1048
        qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1049
        g_free(path);
M
Michael S. Tsirkin 已提交
1050
    }
1051 1052 1053 1054
}

static void device_class_init(ObjectClass *class, void *data)
{
1055 1056
    DeviceClass *dc = DEVICE_CLASS(class);

1057
    class->unparent = device_unparent;
1058
    dc->realize = device_realize;
1059
    dc->unrealize = device_unrealize;
1060 1061 1062 1063 1064 1065 1066 1067

    /* by default all devices were considered as hotpluggable,
     * so with intent to check it in generic qdev_unplug() /
     * device_set_realized() functions make every device
     * hotpluggable. Devices that shouldn't be hotpluggable,
     * should override it in their class_init()
     */
    dc->hotpluggable = true;
1068 1069
}

1070 1071 1072 1073 1074 1075 1076 1077 1078
void device_reset(DeviceState *dev)
{
    DeviceClass *klass = DEVICE_GET_CLASS(dev);

    if (klass->reset) {
        klass->reset(dev);
    }
}

1079 1080 1081 1082 1083
Object *qdev_get_machine(void)
{
    static Object *dev;

    if (dev == NULL) {
1084
        dev = container_get(object_get_root(), "/machine");
1085 1086 1087 1088 1089
    }

    return dev;
}

1090
static const TypeInfo device_type_info = {
1091 1092 1093
    .name = TYPE_DEVICE,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(DeviceState),
1094
    .instance_init = device_initfn,
1095
    .instance_post_init = device_post_init,
1096
    .instance_finalize = device_finalize,
1097
    .class_base_init = device_class_base_init,
1098
    .class_init = device_class_init,
1099 1100 1101 1102
    .abstract = true,
    .class_size = sizeof(DeviceClass),
};

1103 1104 1105 1106 1107
static void qbus_initfn(Object *obj)
{
    BusState *bus = BUS(obj);

    QTAILQ_INIT(&bus->children);
1108 1109
    object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
                             TYPE_HOTPLUG_HANDLER,
1110
                             (Object **)&bus->hotplug_handler,
1111
                             object_property_allow_set_link,
1112 1113
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             NULL);
1114 1115
    object_property_add_bool(obj, "realized",
                             bus_get_realized, bus_set_realized, NULL);
1116 1117
}

1118 1119 1120 1121 1122
static char *default_bus_get_fw_dev_path(DeviceState *dev)
{
    return g_strdup(object_get_typename(OBJECT(dev)));
}

1123 1124
static void bus_class_init(ObjectClass *class, void *data)
{
1125 1126
    BusClass *bc = BUS_CLASS(class);

1127
    class->unparent = bus_unparent;
1128
    bc->get_fw_dev_path = default_bus_get_fw_dev_path;
1129 1130
}

1131 1132 1133 1134 1135 1136 1137
static void qbus_finalize(Object *obj)
{
    BusState *bus = BUS(obj);

    g_free((char *)bus->name);
}

1138 1139 1140 1141 1142 1143
static const TypeInfo bus_info = {
    .name = TYPE_BUS,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(BusState),
    .abstract = true,
    .class_size = sizeof(BusClass),
1144 1145
    .instance_init = qbus_initfn,
    .instance_finalize = qbus_finalize,
1146
    .class_init = bus_class_init,
1147 1148
};

A
Andreas Färber 已提交
1149
static void qdev_register_types(void)
1150
{
1151
    type_register_static(&bus_info);
1152 1153 1154
    type_register_static(&device_type_info);
}

A
Andreas Färber 已提交
1155
type_init(qdev_register_types)