Commit 57c9fafe authored by Anthony Liguori's avatar Anthony Liguori

qom: move properties from qdev to object

This is mostly code movement although not entirely.  This makes properties part
of the Object base class which means that we can now start using Object in a
meaningful way outside of qdev.
Signed-off-by: default avatarAnthony Liguori <aliguori@us.ibm.com>
parent 0beb4942
......@@ -628,10 +628,10 @@ static void visit_type_int32(Visitor *v, int *value, const char *name, Error **e
visit_type_int(v, &val, name, errp);
}
static void rtc_get_date(DeviceState *dev, Visitor *v, void *opaque,
static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
ISADevice *isa = ISA_DEVICE(dev);
ISADevice *isa = ISA_DEVICE(obj);
RTCState *s = DO_UPCAST(RTCState, dev, isa);
visit_start_struct(v, NULL, "struct tm", name, 0, errp);
......@@ -686,8 +686,8 @@ static int rtc_initfn(ISADevice *dev)
qdev_set_legacy_instance_id(&dev->qdev, base, 2);
qemu_register_reset(rtc_reset, s);
qdev_property_add(&s->dev.qdev, "date", "struct tm",
rtc_get_date, NULL, NULL, s, NULL);
object_property_add(OBJECT(s), "date", "struct tm",
rtc_get_date, NULL, NULL, s, NULL);
return 0;
}
......
......@@ -229,7 +229,7 @@ static void pc_init1(MemoryRegion *system_memory,
dev = pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
if (dev) {
qdev_property_add_child(qdev_get_root(), "vga", dev, NULL);
object_property_add_child(object_get_root(), "vga", OBJECT(dev), NULL);
}
if (xen_enabled()) {
......@@ -267,8 +267,8 @@ static void pc_init1(MemoryRegion *system_memory,
* For now, let's "fix" this by making judicious use of paths. This
* is not generally the right way to do this.
*/
qdev_property_add_child(qdev_resolve_path("/i440fx/piix3", NULL),
"rtc", (DeviceState *)rtc_state, NULL);
object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
"rtc", (Object *)rtc_state, NULL);
} else {
for(i = 0; i < MAX_IDE_BUS; i++) {
ISADevice *dev;
......
......@@ -1517,6 +1517,7 @@ static int pci_unplug_device(DeviceState *qdev)
qerror_report(QERR_DEVICE_NO_HOTPLUG, object_get_typename(OBJECT(dev)));
return -1;
}
object_unparent(OBJECT(dev));
return dev->bus->hotplug(dev->bus->hotplug_qdev, dev,
PCI_HOTPLUG_DISABLED);
}
......
......@@ -277,7 +277,7 @@ static PCIBus *i440fx_common_init(const char *device_name,
address_space_io, 0);
s->bus = b;
qdev_init_nofail(dev);
qdev_property_add_child(qdev_get_root(), "i440fx", dev, NULL);
object_property_add_child(object_get_root(), "i440fx", OBJECT(dev), NULL);
d = pci_create_simple(b, 0, device_name);
*pi440fx_state = DO_UPCAST(PCII440FXState, dev, d);
......@@ -316,7 +316,7 @@ static PCIBus *i440fx_common_init(const char *device_name,
pci_bus_irqs(b, piix3_set_irq, pci_slot_get_pirq, piix3,
PIIX_NUM_PIRQS);
}
qdev_property_add_child(dev, "piix3", &piix3->dev.qdev, NULL);
object_property_add_child(OBJECT(dev), "piix3", OBJECT(piix3), NULL);
piix3->pic = pic;
*isa_bus = DO_UPCAST(ISABus, qbus,
qdev_get_child_bus(&piix3->dev.qdev, "isa.0"));
......
......@@ -610,7 +610,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
pcihost = DO_UPCAST(PCIHostState, busdev, sys);
pcihost->address_space = get_system_memory();
qdev_init_nofail(dev);
qdev_property_add_child(qdev_get_root(), "raven", DEVICE(dev), NULL);
object_property_add_child(object_get_root(), "raven", OBJECT(dev), NULL);
pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
if (pci_bus == NULL) {
fprintf(stderr, "Couldn't create PCI host controller.\n");
......
......@@ -18,9 +18,10 @@ static int print_taddr(DeviceState *dev, Property *prop, char *dest, size_t len)
return snprintf(dest, len, "0x" TARGET_FMT_plx, *ptr);
}
static void get_taddr(DeviceState *dev, Visitor *v, void *opaque,
static void get_taddr(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
target_phys_addr_t *ptr = qdev_get_prop_ptr(dev, prop);
int64_t value;
......@@ -29,9 +30,10 @@ static void get_taddr(DeviceState *dev, Visitor *v, void *opaque,
visit_type_int(v, &value, name, errp);
}
static void set_taddr(DeviceState *dev, Visitor *v, void *opaque,
static void set_taddr(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
target_phys_addr_t *ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......
......@@ -173,30 +173,32 @@ int qdev_device_help(QemuOpts *opts)
return 1;
}
static DeviceState *qdev_get_peripheral(void)
static Object *qdev_get_peripheral(void)
{
static DeviceState *dev;
if (dev == NULL) {
dev = qdev_create(NULL, "container");
qdev_property_add_child(qdev_get_root(), "peripheral", dev, NULL);
object_property_add_child(object_get_root(), "peripheral",
OBJECT(dev), NULL);
qdev_init_nofail(dev);
}
return dev;
return OBJECT(dev);
}
static DeviceState *qdev_get_peripheral_anon(void)
static Object *qdev_get_peripheral_anon(void)
{
static DeviceState *dev;
if (dev == NULL) {
dev = qdev_create(NULL, "container");
qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev, NULL);
object_property_add_child(object_get_root(), "peripheral-anon",
OBJECT(dev), NULL);
qdev_init_nofail(dev);
}
return dev;
return OBJECT(dev);
}
static void qbus_list_bus(DeviceState *dev)
......@@ -455,12 +457,13 @@ DeviceState *qdev_device_add(QemuOpts *opts)
id = qemu_opts_id(opts);
if (id) {
qdev->id = id;
qdev_property_add_child(qdev_get_peripheral(), qdev->id, qdev, NULL);
object_property_add_child(qdev_get_peripheral(), qdev->id,
OBJECT(qdev), NULL);
} else {
static int anon_count;
gchar *name = g_strdup_printf("device[%d]", anon_count++);
qdev_property_add_child(qdev_get_peripheral_anon(), name,
qdev, NULL);
object_property_add_child(qdev_get_peripheral_anon(), name,
OBJECT(qdev), NULL);
g_free(name);
}
if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
......
......@@ -55,9 +55,10 @@ static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
}
static void get_bit(DeviceState *dev, Visitor *v, void *opaque,
static void get_bit(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
uint32_t *p = qdev_get_prop_ptr(dev, prop);
bool value = (*p & qdev_get_prop_mask(prop)) != 0;
......@@ -65,9 +66,10 @@ static void get_bit(DeviceState *dev, Visitor *v, void *opaque,
visit_type_bool(v, &value, name, errp);
}
static void set_bit(DeviceState *dev, Visitor *v, void *opaque,
static void set_bit(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
Error *local_err = NULL;
bool value;
......@@ -118,9 +120,10 @@ static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
return snprintf(dest, len, "%" PRIu8, *ptr);
}
static void get_int8(DeviceState *dev, Visitor *v, void *opaque,
static void get_int8(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int8_t *ptr = qdev_get_prop_ptr(dev, prop);
int64_t value;
......@@ -129,9 +132,10 @@ static void get_int8(DeviceState *dev, Visitor *v, void *opaque,
visit_type_int(v, &value, name, errp);
}
static void set_int8(DeviceState *dev, Visitor *v, void *opaque,
const char *name, Error **errp)
static void set_int8(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int8_t *ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......@@ -224,9 +228,10 @@ static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len
return snprintf(dest, len, "%" PRIu16, *ptr);
}
static void get_int16(DeviceState *dev, Visitor *v, void *opaque,
static void get_int16(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int16_t *ptr = qdev_get_prop_ptr(dev, prop);
int64_t value;
......@@ -235,9 +240,10 @@ static void get_int16(DeviceState *dev, Visitor *v, void *opaque,
visit_type_int(v, &value, name, errp);
}
static void set_int16(DeviceState *dev, Visitor *v, void *opaque,
static void set_int16(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int16_t *ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......@@ -296,9 +302,10 @@ static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len
return snprintf(dest, len, "%" PRIu32, *ptr);
}
static void get_int32(DeviceState *dev, Visitor *v, void *opaque,
static void get_int32(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
int64_t value;
......@@ -307,9 +314,10 @@ static void get_int32(DeviceState *dev, Visitor *v, void *opaque,
visit_type_int(v, &value, name, errp);
}
static void set_int32(DeviceState *dev, Visitor *v, void *opaque,
static void set_int32(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......@@ -433,18 +441,20 @@ static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len
return snprintf(dest, len, "%" PRIu64, *ptr);
}
static void get_int64(DeviceState *dev, Visitor *v, void *opaque,
static void get_int64(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int64_t *ptr = qdev_get_prop_ptr(dev, prop);
visit_type_int(v, ptr, name, errp);
}
static void set_int64(DeviceState *dev, Visitor *v, void *opaque,
static void set_int64(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
int64_t *ptr = qdev_get_prop_ptr(dev, prop);
......@@ -523,9 +533,10 @@ static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len
return snprintf(dest, len, "\"%s\"", *ptr);
}
static void get_string(DeviceState *dev, Visitor *v, void *opaque,
static void get_string(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
char **ptr = qdev_get_prop_ptr(dev, prop);
......@@ -537,9 +548,10 @@ static void get_string(DeviceState *dev, Visitor *v, void *opaque,
}
}
static void set_string(DeviceState *dev, Visitor *v, void *opaque,
static void set_string(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
char **ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......@@ -609,9 +621,10 @@ static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
*ptr ? bdrv_get_device_name(*ptr) : "<null>");
}
static void get_generic(DeviceState *dev, Visitor *v, void *opaque,
static void get_generic(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
void **ptr = qdev_get_prop_ptr(dev, prop);
char buffer[1024];
......@@ -624,9 +637,10 @@ static void get_generic(DeviceState *dev, Visitor *v, void *opaque,
visit_type_str(v, &p, name, errp);
}
static void set_generic(DeviceState *dev, Visitor *v, void *opaque,
static void set_generic(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
Error *local_err = NULL;
char *str;
......@@ -774,9 +788,10 @@ static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
}
}
static void get_vlan(DeviceState *dev, Visitor *v, void *opaque,
static void get_vlan(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
int64_t id;
......@@ -785,9 +800,10 @@ static void get_vlan(DeviceState *dev, Visitor *v, void *opaque,
visit_type_int(v, &id, name, errp);
}
static void set_vlan(DeviceState *dev, Visitor *v, void *opaque,
static void set_vlan(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
Error *local_err = NULL;
......@@ -971,9 +987,10 @@ static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t
}
}
static void get_pci_devfn(DeviceState *dev, Visitor *v, void *opaque,
static void get_pci_devfn(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
char buffer[32];
......
......@@ -222,6 +222,7 @@ void qbus_reset_all_fn(void *opaque)
int qdev_simple_unplug_cb(DeviceState *dev)
{
/* just zap it */
object_unparent(OBJECT(dev));
qdev_free(dev);
return 0;
}
......@@ -243,46 +244,6 @@ void qdev_init_nofail(DeviceState *dev)
}
}
static void qdev_property_del_all(DeviceState *dev)
{
while (!QTAILQ_EMPTY(&dev->properties)) {
DeviceProperty *prop = QTAILQ_FIRST(&dev->properties);
QTAILQ_REMOVE(&dev->properties, prop, node);
if (prop->release) {
prop->release(dev, prop->name, prop->opaque);
}
g_free(prop->name);
g_free(prop->type);
g_free(prop);
}
}
static void qdev_property_del_child(DeviceState *dev, DeviceState *child, Error **errp)
{
DeviceProperty *prop;
QTAILQ_FOREACH(prop, &dev->properties, node) {
if (strstart(prop->type, "child<", NULL) && prop->opaque == child) {
break;
}
}
g_assert(prop != NULL);
QTAILQ_REMOVE(&dev->properties, prop, node);
if (prop->release) {
prop->release(dev, prop->name, prop->opaque);
}
g_free(prop->name);
g_free(prop->type);
g_free(prop);
}
/* Unlink device from bus and free the structure. */
void qdev_free(DeviceState *dev)
{
......@@ -290,8 +251,6 @@ void qdev_free(DeviceState *dev)
Property *prop;
DeviceClass *dc = DEVICE_GET_CLASS(dev);
qdev_property_del_all(dev);
if (dev->state == DEV_STATE_INITIALIZED) {
while (dev->num_child_bus) {
bus = QLIST_FIRST(&dev->child_bus);
......@@ -313,12 +272,6 @@ void qdev_free(DeviceState *dev)
prop->info->free(dev, prop);
}
}
if (dev->parent) {
qdev_property_del_child(dev->parent, dev, NULL);
}
if (dev->ref != 0) {
qerror_report(QERR_DEVICE_IN_USE, dev->id?:"");
}
object_delete(OBJECT(dev));
}
......@@ -569,106 +522,19 @@ char* qdev_get_fw_dev_path(DeviceState *dev)
return strdup(path);
}
char *qdev_get_type(DeviceState *dev, Error **errp)
{
return g_strdup(object_get_typename(OBJECT(dev)));
}
void qdev_ref(DeviceState *dev)
static char *qdev_get_type(Object *obj, Error **errp)
{
dev->ref++;
}
void qdev_unref(DeviceState *dev)
{
g_assert(dev->ref > 0);
dev->ref--;
}
void qdev_property_add(DeviceState *dev, const char *name, const char *type,
DevicePropertyAccessor *get, DevicePropertyAccessor *set,
DevicePropertyRelease *release,
void *opaque, Error **errp)
{
DeviceProperty *prop = g_malloc0(sizeof(*prop));
prop->name = g_strdup(name);
prop->type = g_strdup(type);
prop->get = get;
prop->set = set;
prop->release = release;
prop->opaque = opaque;
QTAILQ_INSERT_TAIL(&dev->properties, prop, node);
}
static DeviceProperty *qdev_property_find(DeviceState *dev, const char *name)
{
DeviceProperty *prop;
QTAILQ_FOREACH(prop, &dev->properties, node) {
if (strcmp(prop->name, name) == 0) {
return prop;
}
}
return NULL;
}
void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
Error **errp)
{
DeviceProperty *prop = qdev_property_find(dev, name);
if (prop == NULL) {
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
return;
}
if (!prop->get) {
error_set(errp, QERR_PERMISSION_DENIED);
} else {
prop->get(dev, v, prop->opaque, name, errp);
}
}
void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
Error **errp)
{
DeviceProperty *prop = qdev_property_find(dev, name);
if (prop == NULL) {
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
return;
}
if (!prop->set) {
error_set(errp, QERR_PERMISSION_DENIED);
} else {
prop->set(dev, v, prop->opaque, name, errp);
}
}
const char *qdev_property_get_type(DeviceState *dev, const char *name, Error **errp)
{
DeviceProperty *prop = qdev_property_find(dev, name);
if (prop == NULL) {
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
return NULL;
}
return prop->type;
return g_strdup(object_get_typename(obj));
}
/**
* Legacy property handling
*/
static void qdev_get_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
char buffer[1024];
......@@ -678,9 +544,10 @@ static void qdev_get_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
visit_type_str(v, &ptr, name, errp);
}
static void qdev_set_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
Error *local_err = NULL;
char *ptr = NULL;
......@@ -720,11 +587,11 @@ void qdev_property_add_legacy(DeviceState *dev, Property *prop,
type = g_strdup_printf("legacy<%s>",
prop->info->legacy_name ?: prop->info->name);
qdev_property_add(dev, name, type,
prop->info->print ? qdev_get_legacy_property : NULL,
prop->info->parse ? qdev_set_legacy_property : NULL,
NULL,
prop, errp);
object_property_add(OBJECT(dev), name, type,
prop->info->print ? qdev_get_legacy_property : NULL,
prop->info->parse ? qdev_set_legacy_property : NULL,
NULL,
prop, errp);
g_free(type);
g_free(name);
......@@ -739,333 +606,10 @@ void qdev_property_add_legacy(DeviceState *dev, Property *prop,
void qdev_property_add_static(DeviceState *dev, Property *prop,
Error **errp)
{
qdev_property_add(dev, prop->name, prop->info->name,
prop->info->get, prop->info->set,
NULL,
prop, errp);
}
DeviceState *qdev_get_root(void)
{
static DeviceState *qdev_root;
if (!qdev_root) {
qdev_root = qdev_create(NULL, "container");
qdev_init_nofail(qdev_root);
}
return qdev_root;
}
static void qdev_get_child_property(DeviceState *dev, Visitor *v, void *opaque,
const char *name, Error **errp)
{
DeviceState *child = opaque;
gchar *path;
path = qdev_get_canonical_path(child);
visit_type_str(v, &path, name, errp);
g_free(path);
}
static void qdev_release_child_property(DeviceState *dev, const char *name,
void *opaque)
{
DeviceState *child = opaque;
qdev_unref(child);
}