qdev: Move bus properties to abstract superclasses

In qdev, each bus in practice identified an abstract superclass, but
this was mostly hidden.  In QOM, instead, these abstract classes are
explicit so we can move bus properties there.

All bus property walks are removed, and all device property walks
are changed to look along the class hierarchy instead.

We would have duplicates if class A defines some properties and its
subclass B does not define any, because class_b->props will be
left equal to class_a->props.

The solution here is to reintroduce the class_base_init TypeInfo
callback, that was present in one of the early QOM versions but
removed (on my request...) before committing.

This breaks global bus properties, an obscure feature when used
with the command-line which is actually useful and used when used by
backwards-compatible machine types.  So this patch also adjusts the
global bus properties in hw/pc_piix.c to refer to the abstract class.

Globals and other properties must be modified in the same patch to
avoid complications related to initialization ordering.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Andreas Färber <afaerber@suse.de>
This commit is contained in:
Paolo Bonzini 2012-03-28 18:12:47 +02:00 committed by Andreas Färber
parent 3cb75a7cba
commit bce544740a
14 changed files with 73 additions and 83 deletions

View File

@ -25,7 +25,6 @@ static Property i2c_props[] = {
static struct BusInfo i2c_bus_info = { static struct BusInfo i2c_bus_info = {
.name = "I2C", .name = "I2C",
.size = sizeof(i2c_bus), .size = sizeof(i2c_bus),
.props = i2c_props,
}; };
static void i2c_bus_pre_save(void *opaque) static void i2c_bus_pre_save(void *opaque)
@ -221,6 +220,7 @@ static void i2c_slave_class_init(ObjectClass *klass, void *data)
DeviceClass *k = DEVICE_CLASS(klass); DeviceClass *k = DEVICE_CLASS(klass);
k->init = i2c_slave_qdev_init; k->init = i2c_slave_qdev_init;
k->bus_info = &i2c_bus_info; k->bus_info = &i2c_bus_info;
k->props = i2c_props;
} }
static TypeInfo i2c_slave_type_info = { static TypeInfo i2c_slave_type_info = {

View File

@ -36,7 +36,6 @@ static struct BusInfo ide_bus_info = {
.name = "IDE", .name = "IDE",
.size = sizeof(IDEBus), .size = sizeof(IDEBus),
.get_fw_dev_path = idebus_get_fw_dev_path, .get_fw_dev_path = idebus_get_fw_dev_path,
.props = ide_props,
}; };
void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id) void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id)
@ -251,6 +250,7 @@ static void ide_device_class_init(ObjectClass *klass, void *data)
DeviceClass *k = DEVICE_CLASS(klass); DeviceClass *k = DEVICE_CLASS(klass);
k->init = ide_qdev_init; k->init = ide_qdev_init;
k->bus_info = &ide_bus_info; k->bus_info = &ide_bus_info;
k->props = ide_props;
} }
static TypeInfo ide_device_type_info = { static TypeInfo ide_device_type_info = {

View File

@ -37,7 +37,6 @@ static Property hda_props[] = {
static struct BusInfo hda_codec_bus_info = { static struct BusInfo hda_codec_bus_info = {
.name = "HDA", .name = "HDA",
.size = sizeof(HDACodecBus), .size = sizeof(HDACodecBus),
.props = hda_props,
}; };
void hda_codec_bus_init(DeviceState *dev, HDACodecBus *bus, void hda_codec_bus_init(DeviceState *dev, HDACodecBus *bus,
@ -1278,6 +1277,7 @@ static void hda_codec_device_class_init(ObjectClass *klass, void *data)
k->init = hda_codec_dev_init; k->init = hda_codec_dev_init;
k->exit = hda_codec_dev_exit; k->exit = hda_codec_dev_exit;
k->bus_info = &hda_codec_bus_info; k->bus_info = &hda_codec_bus_info;
k->props = hda_props;
} }
static TypeInfo hda_codec_device_type_info = { static TypeInfo hda_codec_device_type_info = {

View File

@ -29,6 +29,7 @@
#include "apic.h" #include "apic.h"
#include "pci.h" #include "pci.h"
#include "pci_ids.h" #include "pci_ids.h"
#include "usb.h"
#include "net.h" #include "net.h"
#include "boards.h" #include "boards.h"
#include "ide.h" #include "ide.h"
@ -374,7 +375,7 @@ static QEMUMachine pc_machine_v1_1 = {
.property = "vapic",\ .property = "vapic",\
.value = "off",\ .value = "off",\
},{\ },{\
.driver = "USB",\ .driver = TYPE_USB_DEVICE,\
.property = "full-path",\ .property = "full-path",\
.value = "no",\ .value = "no",\
} }
@ -447,7 +448,7 @@ static QEMUMachine pc_machine_v0_14 = {
#define PC_COMPAT_0_13 \ #define PC_COMPAT_0_13 \
PC_COMPAT_0_14,\ PC_COMPAT_0_14,\
{\ {\
.driver = "PCI",\ .driver = TYPE_PCI_DEVICE,\
.property = "command_serr_enable",\ .property = "command_serr_enable",\
.value = "off",\ .value = "off",\
},{\ },{\
@ -519,7 +520,7 @@ static QEMUMachine pc_machine_v0_12 = {
.property = "vectors",\ .property = "vectors",\
.value = stringify(0),\ .value = stringify(0),\
},{\ },{\
.driver = "PCI",\ .driver = TYPE_PCI_DEVICE,\
.property = "rombar",\ .property = "rombar",\
.value = stringify(0),\ .value = stringify(0),\
} }

View File

@ -62,7 +62,6 @@ struct BusInfo pci_bus_info = {
.get_dev_path = pcibus_get_dev_path, .get_dev_path = pcibus_get_dev_path,
.get_fw_dev_path = pcibus_get_fw_dev_path, .get_fw_dev_path = pcibus_get_fw_dev_path,
.reset = pcibus_reset, .reset = pcibus_reset,
.props = pci_props,
}; };
static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num); static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
@ -2003,6 +2002,7 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
k->unplug = pci_unplug_device; k->unplug = pci_unplug_device;
k->exit = pci_unregister_device; k->exit = pci_unregister_device;
k->bus_info = &pci_bus_info; k->bus_info = &pci_bus_info;
k->props = pci_props;
} }
static TypeInfo pci_device_type_info = { static TypeInfo pci_device_type_info = {

View File

@ -123,7 +123,6 @@ int qdev_device_help(QemuOpts *opts)
const char *driver; const char *driver;
Property *prop; Property *prop;
ObjectClass *klass; ObjectClass *klass;
DeviceClass *info;
driver = qemu_opt_get(opts, "driver"); driver = qemu_opt_get(opts, "driver");
if (driver && !strcmp(driver, "?")) { if (driver && !strcmp(driver, "?")) {
@ -149,30 +148,22 @@ int qdev_device_help(QemuOpts *opts)
if (!klass) { if (!klass) {
return 0; return 0;
} }
info = DEVICE_CLASS(klass); do {
for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
for (prop = info->props; prop && prop->name; prop++) { /*
/* * TODO Properties without a parser are just for dirty hacks.
* TODO Properties without a parser are just for dirty hacks. * qdev_prop_ptr is the only such PropertyInfo. It's marked
* qdev_prop_ptr is the only such PropertyInfo. It's marked * for removal. This conditional should be removed along with
* for removal. This conditional should be removed along with * it.
* it. */
*/
if (!prop->info->set) {
continue; /* no way to set it, don't show */
}
error_printf("%s.%s=%s\n", driver, prop->name,
prop->info->legacy_name ?: prop->info->name);
}
if (info->bus_info) {
for (prop = info->bus_info->props; prop && prop->name; prop++) {
if (!prop->info->set) { if (!prop->info->set) {
continue; /* no way to set it, don't show */ continue; /* no way to set it, don't show */
} }
error_printf("%s.%s=%s\n", driver, prop->name, error_printf("%s.%s=%s\n", driver, prop->name,
prop->info->legacy_name ?: prop->info->name); prop->info->legacy_name ?: prop->info->name);
} }
} klass = object_class_get_parent(klass);
} while (klass != object_class_by_name(TYPE_DEVICE));
return 1; return 1;
} }
@ -482,7 +473,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
static void qbus_print(Monitor *mon, BusState *bus, int indent); static void qbus_print(Monitor *mon, BusState *bus, int indent);
static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
const char *prefix, int indent) int indent)
{ {
if (!props) if (!props)
return; return;
@ -501,7 +492,7 @@ static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
error_free(err); error_free(err);
continue; continue;
} }
qdev_printf("%s-prop: %s = %s\n", prefix, props->name, qdev_printf("%s = %s\n", props->name,
value && *value ? value : "<null>"); value && *value ? value : "<null>");
g_free(value); g_free(value);
} }
@ -509,6 +500,7 @@ static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
static void qdev_print(Monitor *mon, DeviceState *dev, int indent) static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
{ {
ObjectClass *class;
BusState *child; BusState *child;
qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
dev->id ? dev->id : ""); dev->id ? dev->id : "");
@ -519,8 +511,11 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
if (dev->num_gpio_out) { if (dev->num_gpio_out) {
qdev_printf("gpio-out %d\n", dev->num_gpio_out); qdev_printf("gpio-out %d\n", dev->num_gpio_out);
} }
qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent); class = object_get_class(OBJECT(dev));
qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent); do {
qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
class = object_class_get_parent(class);
} while (class != object_class_by_name(TYPE_DEVICE));
if (dev->parent_bus->info->print_dev) if (dev->parent_bus->info->print_dev)
dev->parent_bus->info->print_dev(mon, dev, indent); dev->parent_bus->info->print_dev(mon, dev, indent);
QLIST_FOREACH(child, &dev->child_bus, sibling) { QLIST_FOREACH(child, &dev->child_bus, sibling) {

View File

@ -915,17 +915,18 @@ static Property *qdev_prop_walk(Property *props, const char *name)
static Property *qdev_prop_find(DeviceState *dev, const char *name) static Property *qdev_prop_find(DeviceState *dev, const char *name)
{ {
ObjectClass *class;
Property *prop; Property *prop;
/* device properties */ /* device properties */
prop = qdev_prop_walk(qdev_get_props(dev), name); class = object_get_class(OBJECT(dev));
if (prop) do {
return prop; prop = qdev_prop_walk(DEVICE_CLASS(class)->props, name);
if (prop) {
/* bus properties */ return prop;
prop = qdev_prop_walk(dev->parent_bus->info->props, name); }
if (prop) class = object_class_get_parent(class);
return prop; } while (class != object_class_by_name(TYPE_DEVICE));
return NULL; return NULL;
} }
@ -1145,17 +1146,20 @@ void qdev_prop_register_global_list(GlobalProperty *props)
void qdev_prop_set_globals(DeviceState *dev) void qdev_prop_set_globals(DeviceState *dev)
{ {
GlobalProperty *prop; ObjectClass *class = object_get_class(OBJECT(dev));
QTAILQ_FOREACH(prop, &global_props, next) { do {
if (strcmp(object_get_typename(OBJECT(dev)), prop->driver) != 0 && GlobalProperty *prop;
strcmp(qdev_get_bus_info(dev)->name, prop->driver) != 0) { QTAILQ_FOREACH(prop, &global_props, next) {
continue; if (strcmp(object_class_get_name(class), prop->driver) != 0) {
continue;
}
if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
exit(1);
}
} }
if (qdev_prop_parse(dev, prop->property, prop->value) != 0) { class = object_class_get_parent(class);
exit(1); } while (class);
}
}
} }
static int qdev_add_one_global(QemuOpts *opts, void *opaque) static int qdev_add_one_global(QemuOpts *opts, void *opaque)

View File

@ -45,18 +45,6 @@ const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
return dc->vmsd; return dc->vmsd;
} }
BusInfo *qdev_get_bus_info(DeviceState *dev)
{
DeviceClass *dc = DEVICE_GET_CLASS(dev);
return dc->bus_info;
}
Property *qdev_get_props(DeviceState *dev)
{
DeviceClass *dc = DEVICE_GET_CLASS(dev);
return dc->props;
}
const char *qdev_fw_name(DeviceState *dev) const char *qdev_fw_name(DeviceState *dev)
{ {
DeviceClass *dc = DEVICE_GET_CLASS(dev); DeviceClass *dc = DEVICE_GET_CLASS(dev);
@ -78,20 +66,12 @@ static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
void qdev_set_parent_bus(DeviceState *dev, BusState *bus) void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
{ {
Property *prop;
if (qdev_hotplug) { if (qdev_hotplug) {
assert(bus->allow_hotplug); assert(bus->allow_hotplug);
} }
dev->parent_bus = bus; dev->parent_bus = bus;
QTAILQ_INSERT_HEAD(&bus->children, dev, sibling); QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
qdev_property_add_legacy(dev, prop, NULL);
qdev_property_add_static(dev, prop, NULL);
}
qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
} }
/* Create a new device. This only initializes the device state structure /* Create a new device. This only initializes the device state structure
@ -618,6 +598,7 @@ void qdev_property_add_static(DeviceState *dev, Property *prop,
static void device_initfn(Object *obj) static void device_initfn(Object *obj)
{ {
DeviceState *dev = DEVICE(obj); DeviceState *dev = DEVICE(obj);
ObjectClass *class;
Property *prop; Property *prop;
if (qdev_hotplug) { if (qdev_hotplug) {
@ -628,12 +609,15 @@ static void device_initfn(Object *obj)
dev->instance_id_alias = -1; dev->instance_id_alias = -1;
dev->state = DEV_STATE_CREATED; dev->state = DEV_STATE_CREATED;
for (prop = qdev_get_props(dev); prop && prop->name; prop++) { class = object_get_class(OBJECT(dev));
qdev_property_add_legacy(dev, prop, NULL); do {
qdev_property_add_static(dev, prop, NULL); for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
} qdev_property_add_legacy(dev, prop, NULL);
qdev_property_add_static(dev, prop, NULL);
qdev_prop_set_defaults(dev, qdev_get_props(dev)); }
qdev_prop_set_defaults(dev, DEVICE_CLASS(class)->props);
class = object_class_get_parent(class);
} while (class != object_class_by_name(TYPE_DEVICE));
} }
/* Unlink device from bus and free the structure. */ /* Unlink device from bus and free the structure. */
@ -661,6 +645,16 @@ static void device_finalize(Object *obj)
QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling); QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
} }
static void device_class_base_init(ObjectClass *class, void *data)
{
DeviceClass *klass = DEVICE_CLASS(class);
/* We explicitly look up properties in the superclasses,
* so do not propagate them to the subclasses.
*/
klass->props = NULL;
}
void device_reset(DeviceState *dev) void device_reset(DeviceState *dev)
{ {
DeviceClass *klass = DEVICE_GET_CLASS(dev); DeviceClass *klass = DEVICE_GET_CLASS(dev);
@ -687,6 +681,7 @@ static TypeInfo device_type_info = {
.instance_size = sizeof(DeviceState), .instance_size = sizeof(DeviceState),
.instance_init = device_initfn, .instance_init = device_initfn,
.instance_finalize = device_finalize, .instance_finalize = device_finalize,
.class_base_init = device_class_base_init,
.abstract = true, .abstract = true,
.class_size = sizeof(DeviceClass), .class_size = sizeof(DeviceClass),
}; };

View File

@ -96,7 +96,6 @@ struct BusInfo {
bus_get_dev_path get_dev_path; bus_get_dev_path get_dev_path;
bus_get_fw_dev_path get_fw_dev_path; bus_get_fw_dev_path get_fw_dev_path;
qbus_resetfn *reset; qbus_resetfn *reset;
Property *props;
}; };
struct BusState { struct BusState {
@ -347,10 +346,6 @@ const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
const char *qdev_fw_name(DeviceState *dev); const char *qdev_fw_name(DeviceState *dev);
BusInfo *qdev_get_bus_info(DeviceState *dev);
Property *qdev_get_props(DeviceState *dev);
Object *qdev_get_machine(void); Object *qdev_get_machine(void);
/* FIXME: make this a link<> */ /* FIXME: make this a link<> */

View File

@ -24,7 +24,6 @@ static struct BusInfo scsi_bus_info = {
.size = sizeof(SCSIBus), .size = sizeof(SCSIBus),
.get_dev_path = scsibus_get_dev_path, .get_dev_path = scsibus_get_dev_path,
.get_fw_dev_path = scsibus_get_fw_dev_path, .get_fw_dev_path = scsibus_get_fw_dev_path,
.props = scsi_props,
}; };
static int next_scsi_bus; static int next_scsi_bus;
@ -1601,6 +1600,7 @@ static void scsi_device_class_init(ObjectClass *klass, void *data)
k->init = scsi_qdev_init; k->init = scsi_qdev_init;
k->unplug = qdev_simple_unplug_cb; k->unplug = qdev_simple_unplug_cb;
k->exit = scsi_qdev_exit; k->exit = scsi_qdev_exit;
k->props = scsi_props;
} }
static TypeInfo scsi_device_type_info = { static TypeInfo scsi_device_type_info = {

View File

@ -57,7 +57,6 @@ static Property spapr_vio_props[] = {
static struct BusInfo spapr_vio_bus_info = { static struct BusInfo spapr_vio_bus_info = {
.name = "spapr-vio", .name = "spapr-vio",
.size = sizeof(VIOsPAPRBus), .size = sizeof(VIOsPAPRBus),
.props = spapr_vio_props,
}; };
VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg) VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
@ -797,6 +796,7 @@ static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
k->init = spapr_vio_busdev_init; k->init = spapr_vio_busdev_init;
k->reset = spapr_vio_busdev_reset; k->reset = spapr_vio_busdev_reset;
k->bus_info = &spapr_vio_bus_info; k->bus_info = &spapr_vio_bus_info;
k->props = spapr_vio_props;
} }
static TypeInfo spapr_vio_type_info = { static TypeInfo spapr_vio_type_info = {

View File

@ -24,7 +24,6 @@ static struct BusInfo usb_bus_info = {
.print_dev = usb_bus_dev_print, .print_dev = usb_bus_dev_print,
.get_dev_path = usb_get_dev_path, .get_dev_path = usb_get_dev_path,
.get_fw_dev_path = usb_get_fw_dev_path, .get_fw_dev_path = usb_get_fw_dev_path,
.props = usb_props,
}; };
static int next_usb_bus = 0; static int next_usb_bus = 0;
@ -583,6 +582,7 @@ static void usb_device_class_init(ObjectClass *klass, void *data)
k->init = usb_qdev_init; k->init = usb_qdev_init;
k->unplug = qdev_simple_unplug_cb; k->unplug = qdev_simple_unplug_cb;
k->exit = usb_qdev_exit; k->exit = usb_qdev_exit;
k->props = usb_props;
} }
static TypeInfo usb_device_type_info = { static TypeInfo usb_device_type_info = {

View File

@ -1063,7 +1063,6 @@ static Property ccid_props[] = {
static struct BusInfo ccid_bus_info = { static struct BusInfo ccid_bus_info = {
.name = "ccid-bus", .name = "ccid-bus",
.size = sizeof(CCIDBus), .size = sizeof(CCIDBus),
.props = ccid_props,
}; };
void ccid_card_send_apdu_to_guest(CCIDCardState *card, void ccid_card_send_apdu_to_guest(CCIDCardState *card,
@ -1347,6 +1346,7 @@ static void ccid_card_class_init(ObjectClass *klass, void *data)
k->bus_info = &ccid_bus_info; k->bus_info = &ccid_bus_info;
k->init = ccid_card_init; k->init = ccid_card_init;
k->exit = ccid_card_exit; k->exit = ccid_card_exit;
k->props = ccid_props;
} }
static TypeInfo ccid_card_type_info = { static TypeInfo ccid_card_type_info = {

View File

@ -738,7 +738,6 @@ static struct BusInfo virtser_bus_info = {
.name = "virtio-serial-bus", .name = "virtio-serial-bus",
.size = sizeof(VirtIOSerialBus), .size = sizeof(VirtIOSerialBus),
.print_dev = virtser_bus_dev_print, .print_dev = virtser_bus_dev_print,
.props = virtser_props,
}; };
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
@ -985,6 +984,7 @@ static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
k->bus_info = &virtser_bus_info; k->bus_info = &virtser_bus_info;
k->exit = virtser_port_qdev_exit; k->exit = virtser_port_qdev_exit;
k->unplug = qdev_simple_unplug_cb; k->unplug = qdev_simple_unplug_cb;
k->props = virtser_props;
} }
static TypeInfo virtio_serial_port_type_info = { static TypeInfo virtio_serial_port_type_info = {