From nobody Wed Oct 23 00:36:52 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1726710078; cv=none; d=zohomail.com; s=zohoarc; b=Rv+Eiyip1dmdnJ0TQeZBJZ5OFXB8Qh58Qj2mA4qtaS8FyLV1rE6SzVeLemQufEv5WrtDHSszH2tgNi/jX9Lqmfp/yZsDwmbFNpmShJO1hPjCHsMkSWfCf3znPKeQQneqW5GZ+OBq5vSQpib6HXVnrMpIBM5d4rab6wargZkBgS8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1726710078; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=5x7O3DEm9/ZlqDKSkidpImrJVjIndLu/fc4yfWBQuEs=; b=aL+UNIrfMB1vq1oQWgvoZhLxX4p0JFSTKBI33ysXJ/66nQQQ4eEhJBFwTV4wdI3vjCREXwqmmOmcbZdshN8FEiTeGtn9Lo0r4eMuqhrl3nVU6nkzslaV70wn5kNevemaRDz+DEAREN16u6klV4ko8DtlNr/WBGENpfhnjIPTdWo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1726710078474390.16324600381097; Wed, 18 Sep 2024 18:41:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sr69j-0007OM-3r; Wed, 18 Sep 2024 21:40:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sr69f-00075p-RY; Wed, 18 Sep 2024 21:40:24 -0400 Received: from mgamail.intel.com ([192.198.163.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sr69d-0004sM-0M; Wed, 18 Sep 2024 21:40:23 -0400 Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2024 18:40:18 -0700 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.36]) by orviesa006.jf.intel.com with ESMTP; 18 Sep 2024 18:40:12 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1726710021; x=1758246021; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=573w2DHmYSHZ5HLG8xM/Ws3MSekemg/qhlyIWizCQb0=; b=k89lWRCUwm+9dH3RLxrLPrP0k4+RvNnRCTSt2oTw826HwLnAEr8m3K7c hL2tRI++Ysx1Xpgtz8gnBP0tyAmi4NdeSNqj0R95d/afxYP7fTZl3z1Rm qITLEskfCj9OcOYLRvY1EzzdJtVAGr6oI8G9yxv+rt8/IHtsEkZqofE6G nt+J4qMd5C44IYtfglNP4rSTKvcW0FQ2E6ewe5obDgaZVj88b5DIby+8n yAU56Pn1/JFtw8ihUhxIXzIqs5fG5mFu41DAJ/t1lNy3gjYLBhJv1oPZz sUYquDAz9ZxUycVKpfAOMU2eFwLDFZNLBcbBCBtJojL6axVzEAsqBAaaz A==; X-CSE-ConnectionGUID: rBlcq/lUT62CwOLPTV1Q+g== X-CSE-MsgGUID: 7Mx2LXU0TsaCe21LUGTSmg== X-IronPort-AV: E=McAfee;i="6700,10204,11199"; a="25797873" X-IronPort-AV: E=Sophos;i="6.10,240,1719903600"; d="scan'208";a="25797873" X-CSE-ConnectionGUID: VTAf9j9wSdmPPMQUWsdetg== X-CSE-MsgGUID: O69kmvrZS0GmljFMm/GWdA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,240,1719903600"; d="scan'208";a="70058690" From: Zhao Liu To: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Igor Mammedov , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , "Michael S . Tsirkin" , Paolo Bonzini , Richard Henderson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Nicholas Piggin , =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barrat?= , Daniel Henrique Barboza , David Gibson , Harsh Prateek Bora , Markus Armbruster , Marcelo Tosatti , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Peter Maydell Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-ppc@nongnu.org, qemu-arm@nongnu.org, Zhenyu Wang , Dapeng Mi , Yongwei Ma , Zhao Liu Subject: [RFC v2 05/15] qdev: Add method in BusClass to customize device index Date: Thu, 19 Sep 2024 09:55:23 +0800 Message-Id: <20240919015533.766754-6-zhao1.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240919015533.766754-1-zhao1.liu@intel.com> References: <20240919015533.766754-1-zhao1.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=192.198.163.15; envelope-from=zhao1.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1726710080767116600 Content-Type: text/plain; charset="utf-8" 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 --- 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(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index ff073cbff56d..e3e9f0f303d6 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -78,11 +78,17 @@ static void bus_remove_child(BusState *bus, DeviceState= *child) =20 static void bus_add_child(BusState *bus, DeviceState *child) { + BusClass *bc =3D BUS_GET_CLASS(bus); char name[32]; BusChild *kid =3D g_malloc0(sizeof(*kid)); =20 + if (bc->assign_free_index) { + kid->index =3D bc->assign_free_index(bus); + } else { + kid->index =3D bus->max_index++; + } + bus->num_children++; - kid->index =3D bus->max_index++; kid->child =3D child; child->bus_node =3D kid; object_ref(OBJECT(kid->child)); diff --git a/hw/cpu/cpu-topology.c b/hw/cpu/cpu-topology.c index e68c06132e7d..3e8982ff7e6c 100644 --- a/hw/cpu/cpu-topology.c +++ b/hw/cpu/cpu-topology.c @@ -49,11 +49,40 @@ static bool cpu_bus_check_address(BusState *bus, Device= State *dev, return cpu_parent_check_topology(bus->parent, dev, errp); } =20 +static int cpu_bus_assign_free_index(BusState *bus) +{ + BusChild *kid; + int index; + + if (bus->num_children =3D=3D bus->max_index) { + return bus->max_index++; + } + + assert(bus->num_children < bus->max_index); + /* TODO: Introduce the list sorted by index */ + for (index =3D 0; index < bus->num_children; index++) { + bool existed =3D false; + + QTAILQ_FOREACH(kid, &bus->children, sibling) { + if (kid->index =3D=3D index) { + existed =3D true; + break; + } + } + + if (!existed) { + break; + } + } + return index; +} + static void cpu_bus_class_init(ObjectClass *oc, void *data) { BusClass *bc =3D BUS_CLASS(oc); =20 bc->check_address =3D cpu_bus_check_address; + bc->assign_free_index =3D cpu_bus_assign_free_index; } =20 static const TypeInfo cpu_bus_type_info =3D { @@ -177,3 +206,11 @@ int cpu_topo_get_instances_num(CPUTopoState *topo) =20 return bus ? bus->num_children : 1; } + +int cpu_topo_get_index(CPUTopoState *topo) +{ + BusChild *node =3D DEVICE(topo)->bus_node; + + assert(node); + return node->index; +} diff --git a/include/hw/cpu/cpu-topology.h b/include/hw/cpu/cpu-topology.h index 7a447ad16ee7..80aeff18baa3 100644 --- a/include/hw/cpu/cpu-topology.h +++ b/include/hw/cpu/cpu-topology.h @@ -64,5 +64,6 @@ struct CPUTopoState { #define GET_CPU_TOPO_LEVEL(topo) (CPU_TOPO_GET_CLASS(topo)->level) =20 int cpu_topo_get_instances_num(CPUTopoState *topo); +int cpu_topo_get_index(CPUTopoState *topo); =20 #endif /* CPU_TOPO_H */ diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 7cbc5fb97298..77223b28c788 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -342,6 +342,8 @@ struct BusClass { */ bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp); =20 + int (*assign_free_index)(BusState *bus); + BusRealize realize; BusUnrealize unrealize; =20 --=20 2.34.1