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