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