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