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