qdev.c 30.3 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 342 343 344 345 346
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);

    gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
                                     dev, n);
    gpio_list->num_in += n;
}

P
Paul Brook 已提交
347 348
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
{
P
Peter Crosthwaite 已提交
349 350 351 352 353 354 355 356 357 358 359
    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);

    assert(gpio_list->num_out == 0);
    gpio_list->num_out = n;
    gpio_list->out = pins;
P
Paul Brook 已提交
360 361 362 363
}

void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
{
P
Peter Crosthwaite 已提交
364 365 366 367 368 369 370 371 372
    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 已提交
373 374 375 376
}

qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
{
P
Peter Crosthwaite 已提交
377 378 379 380 381 382 383 384 385 386
    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 已提交
387 388 389 390
}

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

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

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

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

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

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

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

437 438 439
    return 0;
}

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

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

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

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

470 471 472
    return 0;
}

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

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

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

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

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

503 504 505 506
    bus->parent = parent;

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

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

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

529 530 531 532
    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);
533
        object_unref(OBJECT(bus));
534
    } else if (bus != sysbus_get_default()) {
535 536 537
        /* 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 已提交
538
    }
539 540
}

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

    while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
        DeviceState *dev = kid->child;
548
        object_unparent(OBJECT(dev));
549 550 551 552 553 554 555 556 557 558 559
    }
    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);
    }
}

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

    return bus->realized;
}

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

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

        /* TODO: recursive realization */
580
    } else if (!value && bus->realized) {
581 582 583 584 585 586 587 588 589
        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) {
590 591 592 593
            bc->unrealize(bus, &local_err);
        }
    }

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

599
    bus->realized = value;
600 601
}

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

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

613
    bus = BUS(object_new(typename));
614
    qbus_realize(bus, parent, name);
615

P
Paul Brook 已提交
616
    return bus;
I
Isaku Yamahata 已提交
617 618
}

619 620 621 622 623 624
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 已提交
625
    }
626 627

    return NULL;
G
Gerd Hoffmann 已提交
628 629
}

630 631 632 633 634 635 636 637 638 639 640 641
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;
}

642 643 644 645 646 647 648
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);
649 650 651 652
        d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
        if (!d) {
            d = bus_get_fw_dev_path(dev->parent_bus, dev);
        }
653
        if (d) {
654
            l += snprintf(p + l, size - l, "%s", d);
655
            g_free(d);
656
        } else {
657
            return l;
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
        }
    }
    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';

674
    return g_strdup(path);
675
}
676

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

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

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

    return NULL;
691
}
692 693 694 695 696

/**
 * Legacy property handling
 */

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

703 704
    char buffer[1024];
    char *ptr = buffer;
705

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

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

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

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

736 737 738 739 740 741 742 743 744 745 746 747
    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)
{
748 749 750
    Error *local_err = NULL;
    Object *obj = OBJECT(dev);

751 752 753 754 755 756 757 758 759
    /*
     * 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;
    }

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

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

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

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
/* @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));
}

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

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

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

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

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

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

837 838 839 840
        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
            local_err == NULL) {
            hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                 dev, &local_err);
841 842 843 844 845 846 847 848 849 850 851 852
        } else if (local_err == NULL &&
                   object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
            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);
                }
            }
853 854
        }

855 856 857 858 859
        if (qdev_get_vmsd(dev) && local_err == NULL) {
            vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
                                           dev->instance_id_alias,
                                           dev->alias_required_for_version);
        }
860 861 862 863 864 865 866 867 868
        if (local_err == NULL) {
            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
                object_property_set_bool(OBJECT(bus), true, "realized",
                                         &local_err);
                if (local_err != NULL) {
                    break;
                }
            }
        }
869 870 871
        if (dev->hotplugged && local_err == NULL) {
            device_reset(dev);
        }
872
        dev->pending_deleted_event = false;
873
    } else if (!value && dev->realized) {
874
        Error **local_errp = NULL;
875
        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
876
            local_errp = local_err ? NULL : &local_err;
877
            object_property_set_bool(OBJECT(bus), false, "realized",
878
                                     local_errp);
879
        }
880
        if (qdev_get_vmsd(dev)) {
881 882
            vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
        }
883 884 885
        if (dc->unrealize) {
            local_errp = local_err ? NULL : &local_err;
            dc->unrealize(dev, local_errp);
886
        }
887
        dev->pending_deleted_event = true;
888 889 890
    }

    if (local_err != NULL) {
891
        error_propagate(errp, local_err);
892 893 894 895 896 897
        return;
    }

    dev->realized = value;
}

898
static bool device_get_hotpluggable(Object *obj, Error **errp)
899 900 901 902
{
    DeviceClass *dc = DEVICE_GET_CLASS(obj);
    DeviceState *dev = DEVICE(obj);

903 904
    return dc->hotpluggable && (dev->parent_bus == NULL ||
                                dev->parent_bus->allow_hotplug);
905 906
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920
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;
}

921 922 923
static void device_initfn(Object *obj)
{
    DeviceState *dev = DEVICE(obj);
924
    ObjectClass *class;
925 926 927 928 929 930 931 932
    Property *prop;

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

    dev->instance_id_alias = -1;
933
    dev->realized = false;
934

935 936
    object_property_add_bool(obj, "realized",
                             device_get_realized, device_set_realized, NULL);
937 938
    object_property_add_bool(obj, "hotpluggable",
                             device_get_hotpluggable, NULL, NULL);
939 940 941
    object_property_add_bool(obj, "hotplugged",
                             device_get_hotplugged, device_set_hotplugged,
                             &error_abort);
942

943 944 945
    class = object_get_class(OBJECT(dev));
    do {
        for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
946 947
            qdev_property_add_legacy(dev, prop, &error_abort);
            qdev_property_add_static(dev, prop, &error_abort);
948 949 950
        }
        class = object_class_get_parent(class);
    } while (class != object_class_by_name(TYPE_DEVICE));
951

952
    object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
953
                             (Object **)&dev->parent_bus, NULL, 0,
954
                             &error_abort);
P
Peter Crosthwaite 已提交
955
    QLIST_INIT(&dev->gpios);
956 957
}

958 959
static void device_post_init(Object *obj)
{
960 961 962 963 964 965 966
    Error *err = NULL;
    qdev_prop_set_globals(DEVICE(obj), &err);
    if (err) {
        qerror_report_err(err);
        error_free(err);
        exit(EXIT_FAILURE);
    }
967 968
}

969 970 971
/* Unlink device from bus and free the structure.  */
static void device_finalize(Object *obj)
{
P
Peter Crosthwaite 已提交
972 973
    NamedGPIOList *ngl, *next;

974
    DeviceState *dev = DEVICE(obj);
975 976
    if (dev->opts) {
        qemu_opts_del(dev->opts);
977
    }
P
Peter Crosthwaite 已提交
978 979 980

    QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
        QLIST_REMOVE(ngl, node);
981
        qemu_free_irqs(ngl->in, ngl->num_in);
P
Peter Crosthwaite 已提交
982 983 984 985 986 987
        g_free(ngl->name);
        g_free(ngl);
        /* ngl->out irqs are owned by the other end and should not be freed
         * here
         */
    }
988 989
}

990 991 992 993 994 995 996 997
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;
998 999
}

1000
static void device_unparent(Object *obj)
1001 1002
{
    DeviceState *dev = DEVICE(obj);
1003
    BusState *bus;
1004

1005 1006 1007
    if (dev->realized) {
        object_property_set_bool(obj, false, "realized", NULL);
    }
1008 1009
    while (dev->num_child_bus) {
        bus = QLIST_FIRST(&dev->child_bus);
1010
        object_unparent(OBJECT(bus));
1011 1012
    }
    if (dev->parent_bus) {
1013
        bus_remove_child(dev->parent_bus, dev);
1014 1015
        object_unref(OBJECT(dev->parent_bus));
        dev->parent_bus = NULL;
1016
    }
M
Michael S. Tsirkin 已提交
1017

1018
    /* Only send event if the device had been completely realized */
1019
    if (dev->pending_deleted_event) {
1020 1021
        gchar *path = object_get_canonical_path(OBJECT(dev));

W
Wenchao Xia 已提交
1022
        qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1023
        g_free(path);
M
Michael S. Tsirkin 已提交
1024
    }
1025 1026 1027 1028
}

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

1031
    class->unparent = device_unparent;
1032
    dc->realize = device_realize;
1033
    dc->unrealize = device_unrealize;
1034 1035 1036 1037 1038 1039 1040 1041

    /* 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;
1042 1043
}

1044 1045 1046 1047 1048 1049 1050 1051 1052
void device_reset(DeviceState *dev)
{
    DeviceClass *klass = DEVICE_GET_CLASS(dev);

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

1053 1054 1055 1056 1057
Object *qdev_get_machine(void)
{
    static Object *dev;

    if (dev == NULL) {
1058
        dev = container_get(object_get_root(), "/machine");
1059 1060 1061 1062 1063
    }

    return dev;
}

1064
static const TypeInfo device_type_info = {
1065 1066 1067
    .name = TYPE_DEVICE,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(DeviceState),
1068
    .instance_init = device_initfn,
1069
    .instance_post_init = device_post_init,
1070
    .instance_finalize = device_finalize,
1071
    .class_base_init = device_class_base_init,
1072
    .class_init = device_class_init,
1073 1074 1075 1076
    .abstract = true,
    .class_size = sizeof(DeviceClass),
};

1077 1078 1079 1080 1081
static void qbus_initfn(Object *obj)
{
    BusState *bus = BUS(obj);

    QTAILQ_INIT(&bus->children);
1082 1083
    object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
                             TYPE_HOTPLUG_HANDLER,
1084
                             (Object **)&bus->hotplug_handler,
1085
                             object_property_allow_set_link,
1086 1087
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             NULL);
1088 1089
    object_property_add_bool(obj, "realized",
                             bus_get_realized, bus_set_realized, NULL);
1090 1091
}

1092 1093 1094 1095 1096
static char *default_bus_get_fw_dev_path(DeviceState *dev)
{
    return g_strdup(object_get_typename(OBJECT(dev)));
}

1097 1098
static void bus_class_init(ObjectClass *class, void *data)
{
1099 1100
    BusClass *bc = BUS_CLASS(class);

1101
    class->unparent = bus_unparent;
1102
    bc->get_fw_dev_path = default_bus_get_fw_dev_path;
1103 1104
}

1105 1106 1107 1108 1109 1110 1111
static void qbus_finalize(Object *obj)
{
    BusState *bus = BUS(obj);

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

1112 1113 1114 1115 1116 1117
static const TypeInfo bus_info = {
    .name = TYPE_BUS,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(BusState),
    .abstract = true,
    .class_size = sizeof(BusClass),
1118 1119
    .instance_init = qbus_initfn,
    .instance_finalize = qbus_finalize,
1120
    .class_init = bus_class_init,
1121 1122
};

A
Andreas Färber 已提交
1123
static void qdev_register_types(void)
1124
{
1125
    type_register_static(&bus_info);
1126 1127 1128
    type_register_static(&device_type_info);
}

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