]> Git Repo - qemu.git/blame - hw/core/qdev.c
qdev: gpio: Don't allow name share between I and O
[qemu.git] / hw / core / qdev.c
CommitLineData
aae9460e
PB
1/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
20/* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
26 this API directly. */
27
83c9f4ca 28#include "hw/qdev.h"
6b1566cb 29#include "hw/fw-path-provider.h"
9c17d615 30#include "sysemu/sysemu.h"
7b1b5d19 31#include "qapi/error.h"
b4a42f81 32#include "qapi/qmp/qerror.h"
7b1b5d19 33#include "qapi/visitor.h"
0402a5d6 34#include "qapi/qmp/qjson.h"
0ee4de6c 35#include "hw/hotplug.h"
b7454548 36#include "hw/boards.h"
24b699fb 37#include "qapi-event.h"
aae9460e 38
ee46d8a5 39int qdev_hotplug = 0;
0ac8ef71
AW
40static bool qdev_hot_added = false;
41static bool qdev_hot_removed = false;
3418bd25 42
4be9f0d1
AL
43const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
44{
6e008585
AL
45 DeviceClass *dc = DEVICE_GET_CLASS(dev);
46 return dc->vmsd;
4be9f0d1
AL
47}
48
4be9f0d1
AL
49const char *qdev_fw_name(DeviceState *dev)
50{
6e008585 51 DeviceClass *dc = DEVICE_GET_CLASS(dev);
4be9f0d1 52
6e008585
AL
53 if (dc->fw_name) {
54 return dc->fw_name;
4be9f0d1
AL
55 }
56
57 return object_get_typename(OBJECT(dev));
58}
59
ca2cc788
PB
60static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
61 Error **errp);
62
0866aca1 63static void bus_remove_child(BusState *bus, DeviceState *child)
0c17542d 64{
0866aca1
AL
65 BusChild *kid;
66
67 QTAILQ_FOREACH(kid, &bus->children, sibling) {
68 if (kid->child == child) {
69 char name[32];
70
71 snprintf(name, sizeof(name), "child[%d]", kid->index);
72 QTAILQ_REMOVE(&bus->children, kid, sibling);
9d127820
PB
73
74 /* This gives back ownership of kid->child back to us. */
0866aca1 75 object_property_del(OBJECT(bus), name, NULL);
9d127820 76 object_unref(OBJECT(kid->child));
0866aca1
AL
77 g_free(kid);
78 return;
79 }
80 }
81}
82
83static void bus_add_child(BusState *bus, DeviceState *child)
84{
85 char name[32];
86 BusChild *kid = g_malloc0(sizeof(*kid));
0c17542d 87
0c17542d
MA
88 if (qdev_hotplug) {
89 assert(bus->allow_hotplug);
0c17542d 90 }
a5296ca9 91
0866aca1
AL
92 kid->index = bus->max_index++;
93 kid->child = child;
9d127820 94 object_ref(OBJECT(kid->child));
a5296ca9 95
0866aca1
AL
96 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
97
9d127820 98 /* This transfers ownership of kid->child to the property. */
0866aca1
AL
99 snprintf(name, sizeof(name), "child[%d]", kid->index);
100 object_property_add_link(OBJECT(bus), name,
101 object_get_typename(OBJECT(child)),
39f72ef9
SH
102 (Object **)&kid->child,
103 NULL, /* read-only property */
104 0, /* return ownership on prop deletion */
105 NULL);
0866aca1
AL
106}
107
108void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
109{
9fbe6127 110 dev->parent_bus = bus;
62d7ba66 111 object_ref(OBJECT(bus));
0866aca1 112 bus_add_child(bus, dev);
0c17542d
MA
113}
114
aae9460e
PB
115/* Create a new device. This only initializes the device state structure
116 and allows properties to be set. qdev_init should be called to
117 initialize the actual device emulation. */
02e2da45 118DeviceState *qdev_create(BusState *bus, const char *name)
0bcdeda7
BS
119{
120 DeviceState *dev;
121
122 dev = qdev_try_create(bus, name);
123 if (!dev) {
e92714c7 124 if (bus) {
312fd5f2 125 error_report("Unknown device '%s' for bus '%s'", name,
23e3fbec 126 object_get_typename(OBJECT(bus)));
e92714c7 127 } else {
312fd5f2 128 error_report("Unknown device '%s' for default sysbus", name);
e92714c7 129 }
01ed1d52 130 abort();
0bcdeda7
BS
131 }
132
133 return dev;
134}
135
da57febf 136DeviceState *qdev_try_create(BusState *bus, const char *type)
aae9460e 137{
9fbe6127
AL
138 DeviceState *dev;
139
da57febf 140 if (object_class_by_name(type) == NULL) {
4ed658ca
AF
141 return NULL;
142 }
da57febf 143 dev = DEVICE(object_new(type));
9fbe6127
AL
144 if (!dev) {
145 return NULL;
146 }
147
10c4c98a 148 if (!bus) {
68694897 149 bus = sysbus_get_default();
10c4c98a
GH
150 }
151
9fbe6127 152 qdev_set_parent_bus(dev, bus);
b09995ae 153 object_unref(OBJECT(dev));
9fbe6127 154 return dev;
aae9460e
PB
155}
156
157/* Initialize a device. Device properties should be set before calling
158 this function. IRQs and MMIO regions should be connected/mapped after
18cfeb52
MA
159 calling this function.
160 On failure, destroy the device and return negative value.
161 Return 0 on success. */
81a322d4 162int qdev_init(DeviceState *dev)
aae9460e 163{
249d4172 164 Error *local_err = NULL;
959f733a 165
7983c8a3 166 assert(!dev->realized);
6e008585 167
249d4172
AF
168 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
169 if (local_err != NULL) {
cffc5113 170 qerror_report_err(local_err);
249d4172 171 error_free(local_err);
02a5c4c9 172 object_unparent(OBJECT(dev));
249d4172 173 return -1;
18cfeb52 174 }
249d4172
AF
175 return 0;
176}
da57febf 177
a7737e44 178static void device_realize(DeviceState *dev, Error **errp)
249d4172
AF
179{
180 DeviceClass *dc = DEVICE_GET_CLASS(dev);
da57febf 181
249d4172
AF
182 if (dc->init) {
183 int rc = dc->init(dev);
184 if (rc < 0) {
a7737e44 185 error_setg(errp, "Device initialization failed.");
249d4172
AF
186 return;
187 }
5ab28c83 188 }
02e2da45
PB
189}
190
fe6c2117
AF
191static void device_unrealize(DeviceState *dev, Error **errp)
192{
193 DeviceClass *dc = DEVICE_GET_CLASS(dev);
194
195 if (dc->exit) {
196 int rc = dc->exit(dev);
197 if (rc < 0) {
198 error_setg(errp, "Device exit failed.");
199 return;
200 }
201 }
202}
203
4d2ffa08
JK
204void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
205 int required_for_version)
206{
7983c8a3 207 assert(!dev->realized);
4d2ffa08
JK
208 dev->instance_id_alias = alias_id;
209 dev->alias_required_for_version = required_for_version;
210}
211
56f9107e 212void qdev_unplug(DeviceState *dev, Error **errp)
3418bd25 213{
6e008585
AL
214 DeviceClass *dc = DEVICE_GET_CLASS(dev);
215
120dc38f 216 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
56f9107e
LC
217 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
218 return;
3418bd25 219 }
593831de 220
1a37eca1
IM
221 if (!dc->hotpluggable) {
222 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
223 object_get_typename(OBJECT(dev)));
224 return;
225 }
226
0ac8ef71
AW
227 qdev_hot_removed = true;
228
5e954943
IM
229 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
230 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
231 } else {
232 assert(dc->unplug != NULL);
233 if (dc->unplug(dev) < 0) { /* legacy handler */
234 error_set(errp, QERR_UNDEFINED_ERROR);
235 }
56f9107e 236 }
3418bd25
GH
237}
238
ec990eb6
AL
239static int qdev_reset_one(DeviceState *dev, void *opaque)
240{
94afdadc 241 device_reset(dev);
ec990eb6
AL
242
243 return 0;
244}
245
b4694b7c
IY
246static int qbus_reset_one(BusState *bus, void *opaque)
247{
0d936928
AL
248 BusClass *bc = BUS_GET_CLASS(bus);
249 if (bc->reset) {
dcc20931 250 bc->reset(bus);
b4694b7c
IY
251 }
252 return 0;
253}
254
5af0a04b
IY
255void qdev_reset_all(DeviceState *dev)
256{
dcc20931 257 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
5af0a04b
IY
258}
259
d0508c36
PB
260void qbus_reset_all(BusState *bus)
261{
dcc20931 262 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
d0508c36
PB
263}
264
80376c3f
IY
265void qbus_reset_all_fn(void *opaque)
266{
267 BusState *bus = opaque;
d0508c36 268 qbus_reset_all(bus);
80376c3f
IY
269}
270
3418bd25
GH
271/* can be used as ->unplug() callback for the simple cases */
272int qdev_simple_unplug_cb(DeviceState *dev)
273{
274 /* just zap it */
02a5c4c9 275 object_unparent(OBJECT(dev));
3418bd25
GH
276 return 0;
277}
278
3b29a101
MT
279
280/* Like qdev_init(), but terminate program via error_report() instead of
e23a1b33
MA
281 returning an error value. This is okay during machine creation.
282 Don't use for hotplug, because there callers need to recover from
283 failure. Exception: if you know the device's init() callback can't
284 fail, then qdev_init_nofail() can't fail either, and is therefore
285 usable even then. But relying on the device implementation that
286 way is somewhat unclean, and best avoided. */
287void qdev_init_nofail(DeviceState *dev)
288{
7de3abe5
AL
289 const char *typename = object_get_typename(OBJECT(dev));
290
bd6c9a61 291 if (qdev_init(dev) < 0) {
7de3abe5 292 error_report("Initialization of device %s failed", typename);
bd6c9a61
MA
293 exit(1);
294 }
e23a1b33
MA
295}
296
3418bd25
GH
297void qdev_machine_creation_done(void)
298{
299 /*
300 * ok, initial machine setup is done, starting from now we can
301 * only create hotpluggable devices
302 */
303 qdev_hotplug = 1;
304}
305
0ac8ef71
AW
306bool qdev_machine_modified(void)
307{
308 return qdev_hot_added || qdev_hot_removed;
309}
310
02e2da45 311BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 312{
02e2da45 313 return dev->parent_bus;
aae9460e
PB
314}
315
a5f54290
PC
316static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
317 const char *name)
318{
319 NamedGPIOList *ngl;
320
321 QLIST_FOREACH(ngl, &dev->gpios, node) {
322 /* NULL is a valid and matchable name, otherwise do a normal
323 * strcmp match.
324 */
325 if ((!ngl->name && !name) ||
326 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
327 return ngl;
328 }
329 }
330
331 ngl = g_malloc0(sizeof(*ngl));
332 ngl->name = g_strdup(name);
333 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
334 return ngl;
335}
336
337void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
338 const char *name, int n)
339{
340 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
341
b235a71f 342 assert(gpio_list->num_out == 0 || !name);
a5f54290
PC
343 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
344 dev, n);
345 gpio_list->num_in += n;
346}
347
aae9460e
PB
348void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
349{
a5f54290
PC
350 qdev_init_gpio_in_named(dev, handler, NULL, n);
351}
352
353void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
354 const char *name, int n)
355{
356 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
357
b235a71f 358 assert(gpio_list->num_in == 0 || !name);
a5f54290
PC
359 assert(gpio_list->num_out == 0);
360 gpio_list->num_out = n;
361 gpio_list->out = pins;
aae9460e
PB
362}
363
364void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
365{
a5f54290
PC
366 qdev_init_gpio_out_named(dev, pins, NULL, n);
367}
368
369qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
370{
371 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
372
373 assert(n >= 0 && n < gpio_list->num_in);
374 return gpio_list->in[n];
aae9460e
PB
375}
376
377qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
378{
a5f54290
PC
379 return qdev_get_gpio_in_named(dev, NULL, n);
380}
381
382void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
383 qemu_irq pin)
384{
385 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
386
387 assert(n >= 0 && n < gpio_list->num_out);
388 gpio_list->out[n] = pin;
aae9460e
PB
389}
390
391void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
392{
a5f54290 393 qdev_connect_gpio_out_named(dev, NULL, n, pin);
aae9460e
PB
394}
395
02e2da45 396BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 397{
02e2da45 398 BusState *bus;
4d6ae674 399
72cf2d4f 400 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 401 if (strcmp(name, bus->name) == 0) {
02e2da45 402 return bus;
4d6ae674
PB
403 }
404 }
405 return NULL;
406}
407
0293214b
PB
408int qbus_walk_children(BusState *bus,
409 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
410 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
411 void *opaque)
81699d8a 412{
0866aca1 413 BusChild *kid;
81699d8a
AL
414 int err;
415
0293214b
PB
416 if (pre_busfn) {
417 err = pre_busfn(bus, opaque);
81699d8a
AL
418 if (err) {
419 return err;
420 }
421 }
422
0866aca1 423 QTAILQ_FOREACH(kid, &bus->children, sibling) {
0293214b
PB
424 err = qdev_walk_children(kid->child,
425 pre_devfn, pre_busfn,
426 post_devfn, post_busfn, opaque);
81699d8a
AL
427 if (err < 0) {
428 return err;
429 }
430 }
431
0293214b
PB
432 if (post_busfn) {
433 err = post_busfn(bus, opaque);
434 if (err) {
435 return err;
436 }
437 }
438
81699d8a
AL
439 return 0;
440}
441
0293214b
PB
442int qdev_walk_children(DeviceState *dev,
443 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
444 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
445 void *opaque)
81699d8a
AL
446{
447 BusState *bus;
448 int err;
449
0293214b
PB
450 if (pre_devfn) {
451 err = pre_devfn(dev, opaque);
81699d8a
AL
452 if (err) {
453 return err;
454 }
455 }
456
457 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
0293214b
PB
458 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
459 post_devfn, post_busfn, opaque);
81699d8a
AL
460 if (err < 0) {
461 return err;
462 }
463 }
464
0293214b
PB
465 if (post_devfn) {
466 err = post_devfn(dev, opaque);
467 if (err) {
468 return err;
469 }
470 }
471
81699d8a
AL
472 return 0;
473}
474
a2ee6b4f 475DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25 476{
0866aca1
AL
477 BusChild *kid;
478 DeviceState *ret;
3418bd25
GH
479 BusState *child;
480
0866aca1
AL
481 QTAILQ_FOREACH(kid, &bus->children, sibling) {
482 DeviceState *dev = kid->child;
483
484 if (dev->id && strcmp(dev->id, id) == 0) {
3418bd25 485 return dev;
0866aca1
AL
486 }
487
3418bd25
GH
488 QLIST_FOREACH(child, &dev->child_bus, sibling) {
489 ret = qdev_find_recursive(child, id);
490 if (ret) {
491 return ret;
492 }
493 }
494 }
495 return NULL;
496}
497
013e1182 498static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
02e2da45 499{
ac7d1ba6 500 const char *typename = object_get_typename(OBJECT(bus));
61de3676 501 BusClass *bc;
d271de9f 502 char *buf;
61de3676 503 int i, len, bus_id;
02e2da45 504
013e1182
PB
505 bus->parent = parent;
506
507 if (name) {
508 bus->name = g_strdup(name);
ac7d1ba6 509 } else if (bus->parent && bus->parent->id) {
61de3676
AG
510 /* parent device has id -> use it plus parent-bus-id for bus name */
511 bus_id = bus->parent->num_child_bus;
512
ac7d1ba6 513 len = strlen(bus->parent->id) + 16;
7267c094 514 buf = g_malloc(len);
61de3676 515 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
d271de9f
GH
516 bus->name = buf;
517 } else {
61de3676
AG
518 /* no id -> use lowercase bus type plus global bus-id for bus name */
519 bc = BUS_GET_CLASS(bus);
520 bus_id = bc->automatic_ids++;
521
0d936928 522 len = strlen(typename) + 16;
7267c094 523 buf = g_malloc(len);
61de3676
AG
524 len = snprintf(buf, len, "%s.%d", typename, bus_id);
525 for (i = 0; i < len; i++) {
bb87ece5 526 buf[i] = qemu_tolower(buf[i]);
61de3676 527 }
d271de9f
GH
528 bus->name = buf;
529 }
530
ac7d1ba6
AL
531 if (bus->parent) {
532 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
533 bus->parent->num_child_bus++;
534 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
b09995ae 535 object_unref(OBJECT(bus));
8185d216 536 } else if (bus != sysbus_get_default()) {
80376c3f
IY
537 /* TODO: once all bus devices are qdevified,
538 only reset handler for main_system_bus should be registered here. */
539 qemu_register_reset(qbus_reset_all_fn, bus);
02e2da45 540 }
cd739fb6
GH
541}
542
6853d27a
PB
543static void bus_unparent(Object *obj)
544{
545 BusState *bus = BUS(obj);
546 BusChild *kid;
547
548 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
549 DeviceState *dev = kid->child;
02a5c4c9 550 object_unparent(OBJECT(dev));
6853d27a
PB
551 }
552 if (bus->parent) {
553 QLIST_REMOVE(bus, sibling);
554 bus->parent->num_child_bus--;
555 bus->parent = NULL;
556 } else {
557 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
558 qemu_unregister_reset(qbus_reset_all_fn, bus);
559 }
560}
561
a7737e44 562static bool bus_get_realized(Object *obj, Error **errp)
02e7f85d
BD
563{
564 BusState *bus = BUS(obj);
565
566 return bus->realized;
567}
568
a7737e44 569static void bus_set_realized(Object *obj, bool value, Error **errp)
02e7f85d
BD
570{
571 BusState *bus = BUS(obj);
572 BusClass *bc = BUS_GET_CLASS(bus);
5942a190 573 BusChild *kid;
02e7f85d
BD
574 Error *local_err = NULL;
575
576 if (value && !bus->realized) {
577 if (bc->realize) {
578 bc->realize(bus, &local_err);
02e7f85d 579 }
5942a190
PB
580
581 /* TODO: recursive realization */
02e7f85d 582 } else if (!value && bus->realized) {
5942a190
PB
583 QTAILQ_FOREACH(kid, &bus->children, sibling) {
584 DeviceState *dev = kid->child;
585 object_property_set_bool(OBJECT(dev), false, "realized",
586 &local_err);
587 if (local_err != NULL) {
588 break;
589 }
590 }
591 if (bc->unrealize && local_err == NULL) {
02e7f85d 592 bc->unrealize(bus, &local_err);
02e7f85d
BD
593 }
594 }
595
b7b34d05
PB
596 if (local_err != NULL) {
597 error_propagate(errp, local_err);
598 return;
599 }
02e7f85d 600
b7b34d05 601 bus->realized = value;
02e7f85d
BD
602}
603
fb17dfe0 604void qbus_create_inplace(void *bus, size_t size, const char *typename,
0d936928 605 DeviceState *parent, const char *name)
cd739fb6 606{
213f0c4f 607 object_initialize(bus, size, typename);
013e1182 608 qbus_realize(bus, parent, name);
02e2da45 609}
cae4956e 610
0d936928 611BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
2da8bb92 612{
cd739fb6
GH
613 BusState *bus;
614
0d936928 615 bus = BUS(object_new(typename));
013e1182 616 qbus_realize(bus, parent, name);
ac7d1ba6 617
02e2da45 618 return bus;
2da8bb92
IY
619}
620
0d936928
AL
621static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
622{
623 BusClass *bc = BUS_GET_CLASS(bus);
624
625 if (bc->get_fw_dev_path) {
626 return bc->get_fw_dev_path(dev);
131ec1bd 627 }
0d936928
AL
628
629 return NULL;
131ec1bd
GH
630}
631
6b1566cb
PB
632static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
633{
634 Object *obj = OBJECT(dev);
635 char *d = NULL;
636
637 while (!d && obj->parent) {
638 obj = obj->parent;
639 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
640 }
641 return d;
642}
643
1ca4d09a
GN
644static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
645{
646 int l = 0;
647
648 if (dev && dev->parent_bus) {
649 char *d;
650 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
6b1566cb
PB
651 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
652 if (!d) {
653 d = bus_get_fw_dev_path(dev->parent_bus, dev);
654 }
0d936928 655 if (d) {
1ca4d09a 656 l += snprintf(p + l, size - l, "%s", d);
7267c094 657 g_free(d);
1ca4d09a 658 } else {
bbfa18fc 659 return l;
1ca4d09a
GN
660 }
661 }
662 l += snprintf(p + l , size - l, "/");
663
664 return l;
665}
666
667char* qdev_get_fw_dev_path(DeviceState *dev)
668{
669 char path[128];
670 int l;
671
672 l = qdev_get_fw_dev_path_helper(dev, path, 128);
673
674 path[l-1] = '\0';
675
a5cf8262 676 return g_strdup(path);
1ca4d09a 677}
85ed303b 678
09e5ab63 679char *qdev_get_dev_path(DeviceState *dev)
85ed303b 680{
0d936928 681 BusClass *bc;
09e5ab63
AL
682
683 if (!dev || !dev->parent_bus) {
684 return NULL;
685 }
686
0d936928
AL
687 bc = BUS_GET_CLASS(dev->parent_bus);
688 if (bc->get_dev_path) {
689 return bc->get_dev_path(dev);
09e5ab63
AL
690 }
691
692 return NULL;
44677ded 693}
a5296ca9
AL
694
695/**
696 * Legacy property handling
697 */
698
57c9fafe 699static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
a5296ca9
AL
700 const char *name, Error **errp)
701{
57c9fafe 702 DeviceState *dev = DEVICE(obj);
a5296ca9
AL
703 Property *prop = opaque;
704
e3cb6ba6
PB
705 char buffer[1024];
706 char *ptr = buffer;
a5296ca9 707
e3cb6ba6
PB
708 prop->info->print(dev, prop, buffer, sizeof(buffer));
709 visit_type_str(v, &ptr, name, errp);
a5296ca9
AL
710}
711
a5296ca9
AL
712/**
713 * @qdev_add_legacy_property - adds a legacy property
714 *
715 * Do not use this is new code! Properties added through this interface will
ca2cc788 716 * be given names and types in the "legacy" namespace.
a5296ca9 717 *
68ee3569
PB
718 * Legacy properties are string versions of other OOM properties. The format
719 * of the string depends on the property type.
a5296ca9 720 */
f5a014d2
SW
721static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
722 Error **errp)
a5296ca9 723{
7ce7ffe0 724 gchar *name;
a5296ca9 725
f3be016d 726 /* Register pointer properties as legacy properties */
03ff7770 727 if (!prop->info->print && prop->info->get) {
68ee3569
PB
728 return;
729 }
f3be016d 730
ca2cc788 731 name = g_strdup_printf("legacy-%s", prop->name);
7ce7ffe0 732 object_property_add(OBJECT(dev), name, "str",
68ee3569 733 prop->info->print ? qdev_get_legacy_property : prop->info->get,
03ff7770 734 NULL,
57c9fafe
AL
735 NULL,
736 prop, errp);
a5296ca9 737
ca2cc788
PB
738 g_free(name);
739}
740
741/**
742 * @qdev_property_add_static - add a @Property to a device.
743 *
744 * Static properties access data in a struct. The actual type of the
745 * property and the field depends on the property type.
746 */
747void qdev_property_add_static(DeviceState *dev, Property *prop,
748 Error **errp)
749{
fdae245f
PB
750 Error *local_err = NULL;
751 Object *obj = OBJECT(dev);
752
d822979b
PB
753 /*
754 * TODO qdev_prop_ptr does not have getters or setters. It must
755 * go now that it can be replaced with links. The test should be
756 * removed along with it: all static properties are read/write.
757 */
758 if (!prop->info->get && !prop->info->set) {
759 return;
760 }
761
fdae245f 762 object_property_add(obj, prop->name, prop->info->name,
57c9fafe 763 prop->info->get, prop->info->set,
dd0ba250 764 prop->info->release,
fdae245f
PB
765 prop, &local_err);
766
767 if (local_err) {
768 error_propagate(errp, local_err);
769 return;
770 }
771 if (prop->qtype == QTYPE_NONE) {
772 return;
773 }
774
775 if (prop->qtype == QTYPE_QBOOL) {
5433a0a8 776 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
fdae245f
PB
777 } else if (prop->info->enum_table) {
778 object_property_set_str(obj, prop->info->enum_table[prop->defval],
5433a0a8 779 prop->name, &error_abort);
fdae245f 780 } else if (prop->qtype == QTYPE_QINT) {
5433a0a8 781 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
fdae245f 782 }
6a146eba 783}
1de81d28 784
67cc7e0a
SH
785/* @qdev_alias_all_properties - Add alias properties to the source object for
786 * all qdev properties on the target DeviceState.
787 */
788void qdev_alias_all_properties(DeviceState *target, Object *source)
789{
790 ObjectClass *class;
791 Property *prop;
792
793 class = object_get_class(OBJECT(target));
794 do {
795 DeviceClass *dc = DEVICE_CLASS(class);
796
797 for (prop = dc->props; prop && prop->name; prop++) {
798 object_property_add_alias(source, prop->name,
799 OBJECT(target), prop->name,
800 &error_abort);
801 }
802 class = object_class_get_parent(class);
803 } while (class != object_class_by_name(TYPE_DEVICE));
804}
805
a7737e44 806static bool device_get_realized(Object *obj, Error **errp)
249d4172
AF
807{
808 DeviceState *dev = DEVICE(obj);
809 return dev->realized;
810}
811
a7737e44 812static void device_set_realized(Object *obj, bool value, Error **errp)
249d4172
AF
813{
814 DeviceState *dev = DEVICE(obj);
815 DeviceClass *dc = DEVICE_GET_CLASS(dev);
5c21ce77 816 BusState *bus;
249d4172
AF
817 Error *local_err = NULL;
818
1a37eca1 819 if (dev->hotplugged && !dc->hotpluggable) {
a7737e44 820 error_set(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
1a37eca1
IM
821 return;
822 }
823
249d4172 824 if (value && !dev->realized) {
d578029e 825 if (!obj->parent) {
249d4172
AF
826 static int unattached_count;
827 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
828
829 object_property_add_child(container_get(qdev_get_machine(),
830 "/unattached"),
d578029e 831 name, obj, &error_abort);
249d4172
AF
832 g_free(name);
833 }
834
a7ddba52
IM
835 if (dc->realize) {
836 dc->realize(dev, &local_err);
837 }
838
1d45a705
GA
839 if (local_err != NULL) {
840 goto fail;
841 }
842
843 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
5e954943
IM
844 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
845 dev, &local_err);
1d45a705 846 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
b7454548
IM
847 HotplugHandler *hotplug_ctrl;
848 MachineState *machine = MACHINE(qdev_get_machine());
849 MachineClass *mc = MACHINE_GET_CLASS(machine);
850
851 if (mc->get_hotplug_handler) {
852 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
853 if (hotplug_ctrl) {
854 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
855 }
856 }
5e954943
IM
857 }
858
1d45a705
GA
859 if (local_err != NULL) {
860 goto post_realize_fail;
861 }
862
863 if (qdev_get_vmsd(dev)) {
249d4172
AF
864 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
865 dev->instance_id_alias,
866 dev->alias_required_for_version);
867 }
1d45a705
GA
868
869 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
870 object_property_set_bool(OBJECT(bus), true, "realized",
5c21ce77 871 &local_err);
1d45a705
GA
872 if (local_err != NULL) {
873 goto child_realize_fail;
5c21ce77
BD
874 }
875 }
1d45a705 876 if (dev->hotplugged) {
249d4172
AF
877 device_reset(dev);
878 }
352e8da7 879 dev->pending_deleted_event = false;
249d4172 880 } else if (!value && dev->realized) {
cd4520ad 881 Error **local_errp = NULL;
5c21ce77 882 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
cd4520ad 883 local_errp = local_err ? NULL : &local_err;
5c21ce77 884 object_property_set_bool(OBJECT(bus), false, "realized",
cd4520ad 885 local_errp);
5c21ce77 886 }
cd4520ad 887 if (qdev_get_vmsd(dev)) {
fe6c2117
AF
888 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
889 }
cd4520ad
GA
890 if (dc->unrealize) {
891 local_errp = local_err ? NULL : &local_err;
892 dc->unrealize(dev, local_errp);
249d4172 893 }
352e8da7 894 dev->pending_deleted_event = true;
249d4172
AF
895 }
896
897 if (local_err != NULL) {
1d45a705 898 goto fail;
249d4172
AF
899 }
900
901 dev->realized = value;
1d45a705
GA
902 return;
903
904child_realize_fail:
905 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
906 object_property_set_bool(OBJECT(bus), false, "realized",
907 NULL);
908 }
909
910 if (qdev_get_vmsd(dev)) {
911 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
912 }
913
914post_realize_fail:
915 if (dc->unrealize) {
916 dc->unrealize(dev, NULL);
917 }
918
919fail:
920 error_propagate(errp, local_err);
921 return;
249d4172
AF
922}
923
a7737e44 924static bool device_get_hotpluggable(Object *obj, Error **errp)
1a37eca1
IM
925{
926 DeviceClass *dc = DEVICE_GET_CLASS(obj);
927 DeviceState *dev = DEVICE(obj);
928
2b81b35f
AF
929 return dc->hotpluggable && (dev->parent_bus == NULL ||
930 dev->parent_bus->allow_hotplug);
1a37eca1
IM
931}
932
d012ffc1
IM
933static bool device_get_hotplugged(Object *obj, Error **err)
934{
935 DeviceState *dev = DEVICE(obj);
936
937 return dev->hotplugged;
938}
939
940static void device_set_hotplugged(Object *obj, bool value, Error **err)
941{
942 DeviceState *dev = DEVICE(obj);
943
944 dev->hotplugged = value;
945}
946
9674bfe4
AL
947static void device_initfn(Object *obj)
948{
949 DeviceState *dev = DEVICE(obj);
bce54474 950 ObjectClass *class;
9674bfe4
AL
951 Property *prop;
952
953 if (qdev_hotplug) {
954 dev->hotplugged = 1;
955 qdev_hot_added = true;
956 }
957
958 dev->instance_id_alias = -1;
7983c8a3 959 dev->realized = false;
9674bfe4 960
249d4172
AF
961 object_property_add_bool(obj, "realized",
962 device_get_realized, device_set_realized, NULL);
1a37eca1
IM
963 object_property_add_bool(obj, "hotpluggable",
964 device_get_hotpluggable, NULL, NULL);
d012ffc1
IM
965 object_property_add_bool(obj, "hotplugged",
966 device_get_hotplugged, device_set_hotplugged,
967 &error_abort);
249d4172 968
bce54474
PB
969 class = object_get_class(OBJECT(dev));
970 do {
971 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
5433a0a8
PC
972 qdev_property_add_legacy(dev, prop, &error_abort);
973 qdev_property_add_static(dev, prop, &error_abort);
bce54474 974 }
bce54474
PB
975 class = object_class_get_parent(class);
976 } while (class != object_class_by_name(TYPE_DEVICE));
9674bfe4 977
f968fc68 978 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
39f72ef9 979 (Object **)&dev->parent_bus, NULL, 0,
9561fda8 980 &error_abort);
a5f54290 981 QLIST_INIT(&dev->gpios);
9674bfe4
AL
982}
983
99a0b036
EH
984static void device_post_init(Object *obj)
985{
31962700
EH
986 Error *err = NULL;
987 qdev_prop_set_globals(DEVICE(obj), &err);
988 if (err) {
989 qerror_report_err(err);
990 error_free(err);
991 exit(EXIT_FAILURE);
992 }
99a0b036
EH
993}
994
60adba37
AL
995/* Unlink device from bus and free the structure. */
996static void device_finalize(Object *obj)
997{
a5f54290
PC
998 NamedGPIOList *ngl, *next;
999
60adba37 1000 DeviceState *dev = DEVICE(obj);
06f7f2bb
PB
1001 if (dev->opts) {
1002 qemu_opts_del(dev->opts);
60adba37 1003 }
a5f54290
PC
1004
1005 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1006 QLIST_REMOVE(ngl, node);
f173d57a 1007 qemu_free_irqs(ngl->in, ngl->num_in);
a5f54290
PC
1008 g_free(ngl->name);
1009 g_free(ngl);
1010 /* ngl->out irqs are owned by the other end and should not be freed
1011 * here
1012 */
1013 }
60adba37
AL
1014}
1015
bce54474
PB
1016static void device_class_base_init(ObjectClass *class, void *data)
1017{
1018 DeviceClass *klass = DEVICE_CLASS(class);
1019
1020 /* We explicitly look up properties in the superclasses,
1021 * so do not propagate them to the subclasses.
1022 */
1023 klass->props = NULL;
60adba37
AL
1024}
1025
5d5b24d0 1026static void device_unparent(Object *obj)
667d22d1
PB
1027{
1028 DeviceState *dev = DEVICE(obj);
06f7f2bb 1029 BusState *bus;
667d22d1 1030
5c21ce77
BD
1031 if (dev->realized) {
1032 object_property_set_bool(obj, false, "realized", NULL);
1033 }
06f7f2bb
PB
1034 while (dev->num_child_bus) {
1035 bus = QLIST_FIRST(&dev->child_bus);
6780a22c 1036 object_unparent(OBJECT(bus));
06f7f2bb 1037 }
06f7f2bb 1038 if (dev->parent_bus) {
5d5b24d0 1039 bus_remove_child(dev->parent_bus, dev);
62d7ba66
PB
1040 object_unref(OBJECT(dev->parent_bus));
1041 dev->parent_bus = NULL;
5d5b24d0 1042 }
0402a5d6 1043
b1ee5829 1044 /* Only send event if the device had been completely realized */
352e8da7 1045 if (dev->pending_deleted_event) {
b1ee5829
AL
1046 gchar *path = object_get_canonical_path(OBJECT(dev));
1047
24b699fb 1048 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
b1ee5829 1049 g_free(path);
0402a5d6 1050 }
667d22d1
PB
1051}
1052
1053static void device_class_init(ObjectClass *class, void *data)
1054{
249d4172
AF
1055 DeviceClass *dc = DEVICE_CLASS(class);
1056
5d5b24d0 1057 class->unparent = device_unparent;
249d4172 1058 dc->realize = device_realize;
fe6c2117 1059 dc->unrealize = device_unrealize;
267a3264
IM
1060
1061 /* by default all devices were considered as hotpluggable,
1062 * so with intent to check it in generic qdev_unplug() /
1063 * device_set_realized() functions make every device
1064 * hotpluggable. Devices that shouldn't be hotpluggable,
1065 * should override it in their class_init()
1066 */
1067 dc->hotpluggable = true;
667d22d1
PB
1068}
1069
94afdadc
AL
1070void device_reset(DeviceState *dev)
1071{
1072 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1073
1074 if (klass->reset) {
1075 klass->reset(dev);
1076 }
1077}
1078
f05f6b4a
PB
1079Object *qdev_get_machine(void)
1080{
1081 static Object *dev;
1082
1083 if (dev == NULL) {
dfe47e70 1084 dev = container_get(object_get_root(), "/machine");
f05f6b4a
PB
1085 }
1086
1087 return dev;
1088}
1089
8c43a6f0 1090static const TypeInfo device_type_info = {
32fea402
AL
1091 .name = TYPE_DEVICE,
1092 .parent = TYPE_OBJECT,
1093 .instance_size = sizeof(DeviceState),
9674bfe4 1094 .instance_init = device_initfn,
99a0b036 1095 .instance_post_init = device_post_init,
60adba37 1096 .instance_finalize = device_finalize,
bce54474 1097 .class_base_init = device_class_base_init,
667d22d1 1098 .class_init = device_class_init,
32fea402
AL
1099 .abstract = true,
1100 .class_size = sizeof(DeviceClass),
1101};
1102
ac7d1ba6
AL
1103static void qbus_initfn(Object *obj)
1104{
1105 BusState *bus = BUS(obj);
1106
1107 QTAILQ_INIT(&bus->children);
0ee4de6c
IM
1108 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
1109 TYPE_HOTPLUG_HANDLER,
9561fda8 1110 (Object **)&bus->hotplug_handler,
39f72ef9 1111 object_property_allow_set_link,
9561fda8
SH
1112 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1113 NULL);
02e7f85d
BD
1114 object_property_add_bool(obj, "realized",
1115 bus_get_realized, bus_set_realized, NULL);
ac7d1ba6
AL
1116}
1117
bbfa18fc
AK
1118static char *default_bus_get_fw_dev_path(DeviceState *dev)
1119{
1120 return g_strdup(object_get_typename(OBJECT(dev)));
1121}
1122
6853d27a
PB
1123static void bus_class_init(ObjectClass *class, void *data)
1124{
bbfa18fc
AK
1125 BusClass *bc = BUS_CLASS(class);
1126
6853d27a 1127 class->unparent = bus_unparent;
bbfa18fc 1128 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
6853d27a
PB
1129}
1130
ac7d1ba6
AL
1131static void qbus_finalize(Object *obj)
1132{
1133 BusState *bus = BUS(obj);
ac7d1ba6 1134
ac7d1ba6
AL
1135 g_free((char *)bus->name);
1136}
1137
0d936928
AL
1138static const TypeInfo bus_info = {
1139 .name = TYPE_BUS,
1140 .parent = TYPE_OBJECT,
1141 .instance_size = sizeof(BusState),
1142 .abstract = true,
1143 .class_size = sizeof(BusClass),
ac7d1ba6
AL
1144 .instance_init = qbus_initfn,
1145 .instance_finalize = qbus_finalize,
6853d27a 1146 .class_init = bus_class_init,
0d936928
AL
1147};
1148
83f7d43a 1149static void qdev_register_types(void)
32fea402 1150{
0d936928 1151 type_register_static(&bus_info);
32fea402
AL
1152 type_register_static(&device_type_info);
1153}
1154
83f7d43a 1155type_init(qdev_register_types)
This page took 0.849303 seconds and 4 git commands to generate.