@@ -78,11 +78,17 @@ static void bus_remove_child(BusState *bus, DeviceState *child)
static void bus_add_child(BusState *bus, DeviceState *child)
{
+ BusClass *bc = BUS_GET_CLASS(bus);
char name[32];
BusChild *kid = g_malloc0(sizeof(*kid));
+ if (bc->assign_free_index) {
+ kid->index = bc->assign_free_index(bus);
+ } else {
+ kid->index = bus->max_index++;
+ }
+
bus->num_children++;
- kid->index = bus->max_index++;
kid->child = child;
child->bus_node = kid;
object_ref(OBJECT(kid->child));
@@ -49,11 +49,40 @@ static bool cpu_bus_check_address(BusState *bus, DeviceState *dev,
return cpu_parent_check_topology(bus->parent, dev, errp);
}
+static int cpu_bus_assign_free_index(BusState *bus)
+{
+ BusChild *kid;
+ int index;
+
+ if (bus->num_children == bus->max_index) {
+ return bus->max_index++;
+ }
+
+ assert(bus->num_children < bus->max_index);
+ /* TODO: Introduce the list sorted by index */
+ for (index = 0; index < bus->num_children; index++) {
+ bool existed = false;
+
+ QTAILQ_FOREACH(kid, &bus->children, sibling) {
+ if (kid->index == index) {
+ existed = true;
+ break;
+ }
+ }
+
+ if (!existed) {
+ break;
+ }
+ }
+ return index;
+}
+
static void cpu_bus_class_init(ObjectClass *oc, void *data)
{
BusClass *bc = BUS_CLASS(oc);
bc->check_address = cpu_bus_check_address;
+ bc->assign_free_index = cpu_bus_assign_free_index;
}
static const TypeInfo cpu_bus_type_info = {
@@ -177,3 +206,11 @@ int cpu_topo_get_instances_num(CPUTopoState *topo)
return bus ? bus->num_children : 1;
}
+
+int cpu_topo_get_index(CPUTopoState *topo)
+{
+ BusChild *node = DEVICE(topo)->bus_node;
+
+ assert(node);
+ return node->index;
+}
@@ -64,5 +64,6 @@ struct CPUTopoState {
#define GET_CPU_TOPO_LEVEL(topo) (CPU_TOPO_GET_CLASS(topo)->level)
int cpu_topo_get_instances_num(CPUTopoState *topo);
+int cpu_topo_get_index(CPUTopoState *topo);
#endif /* CPU_TOPO_H */
@@ -342,6 +342,8 @@ struct BusClass {
*/
bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
+ int (*assign_free_index)(BusState *bus);
+
BusRealize realize;
BusUnrealize unrealize;
Currently, when the bus assigns an index to a child device, it relies on a monotonically increasing max_index. However, when a device is removed from the bus, its index is not reassigned to new devices, leading to "holes" in child indices. For topology devices, such as CPUs/cores, arches define custom sub-topology IDs. Some of these IDs are global (e.g., core-id for core devices), while others are local (e.g., thread-id/core-id/module-id for x86 CPUs). Local IDs are indexes under the same parent device and align with BusChild's index meaning. Therefore, local IDs in a topology context should use BusChild.index. Considering that topology devices support hot-plug and local IDs often have range constraints, add a new method (BusClass.assign_free_index) to allow the bus to customize index assignment. Based on this method, the CPU bus will search for free index "holes" created by unplugging and assign these free indices to newly inserted devices. Signed-off-by: Zhao Liu <zhao1.liu@intel.com> --- hw/core/qdev.c | 8 +++++++- hw/cpu/cpu-topology.c | 37 +++++++++++++++++++++++++++++++++++ include/hw/cpu/cpu-topology.h | 1 + include/hw/qdev-core.h | 2 ++ 4 files changed, 47 insertions(+), 1 deletion(-)