From patchwork Thu Nov 30 14:41:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Liu X-Patchwork-Id: 13474561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5EDD0C46CA0 for ; Thu, 30 Nov 2023 14:39:31 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.644763.1006253 (Exim 4.92) (envelope-from ) id 1r8iC5-0002qu-7w; Thu, 30 Nov 2023 14:39:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 644763.1006253; Thu, 30 Nov 2023 14:39:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8iC5-0002nJ-19; Thu, 30 Nov 2023 14:39:09 +0000 Received: by outflank-mailman (input) for mailman id 644763; Thu, 30 Nov 2023 14:32:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8i5Y-0007mE-PZ for xen-devel@lists.xenproject.org; Thu, 30 Nov 2023 14:32:24 +0000 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 469be651-8f8d-11ee-98e4-6d05b1d4d9a1; Thu, 30 Nov 2023 15:32:23 +0100 (CET) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2023 06:31:58 -0800 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.36]) by orsmga005.jf.intel.com with ESMTP; 30 Nov 2023 06:31:48 -0800 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 469be651-8f8d-11ee-98e4-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701354743; x=1732890743; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zcorAWDLgR7QueFdQsmIMfIxo58a5rqxmGG3Y6CdsHI=; b=VgYisIiSsbos+Sam4aKPEPGUxnfFbnknBxJu5NgdrxvxdOzYk3xcsP2e jWmhxxi3IeWau+eWxJdcmLiVsygBG5HHdHPLCFLzkguu3ndjMwryy8417 rTzFGXEpS3w8mgES/x/4RloHdijezqTLmb0eVG4aRcPReDQbEQmo3Up2w 3YbOWHh4m0kANh59wOWaZtGYLcyvZmmk3yXFGX8+eGL/DmcyEeIAS1MLI TldG/h/NPWRpzpPgjJ9fBhCU8fYed0nPnjiFGDgwyWUqSbMav+WuUkjTr inR911vtcxa0aDPELUxW0AHvhQF7byYS4tU51lq0FmJPFCJzwkC/3elgz A==; X-IronPort-AV: E=McAfee;i="6600,9927,10910"; a="479531485" X-IronPort-AV: E=Sophos;i="6.04,239,1695711600"; d="scan'208";a="479531485" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10910"; a="942729745" X-IronPort-AV: E=Sophos;i="6.04,239,1695711600"; d="scan'208";a="942729745" From: Zhao Liu To: Paolo Bonzini , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Philippe_M?= =?utf-8?q?athieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Richard Henderson , "Michael S . Tsirkin" , Jason Wang , Nicholas Piggin , Daniel Henrique Barboza , Igor Mammedov , =?utf-8?q?C=C3=A9dric_Le_Goater?= , =?utf-8?b?RnLDqWTDqXJp?= =?utf-8?b?YyBCYXJyYXQ=?= , David Gibson , Harsh Prateek Bora , Stefano Stabellini , Anthony Perard , Paul Durrant , Gerd Hoffmann , Peter Maydell , Alistair Francis , "Edgar E . Iglesias" , =?utf-8?q?Daniel_P_=2E_Ber?= =?utf-8?q?rang=C3=A9?= , Bin Meng , Palmer Dabbelt , Weiwei Li , Liu Zhiwei , qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-ppc@nongnu.org, xen-devel@lists.xenproject.org, qemu-arm@nongnu.org, qemu-riscv@nongnu.org, qemu-s390x@nongnu.org Cc: Nina Schoetterl-Glausch , Thomas Huth , Zhiyuan Lv , Zhenyu Wang , Yongwei Ma , Zhao Liu Subject: [RFC 09/41] hw/core/topo: Support topology index for topology device Date: Thu, 30 Nov 2023 22:41:31 +0800 Message-Id: <20231130144203.2307629-10-zhao1.liu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130144203.2307629-1-zhao1.liu@linux.intel.com> References: <20231130144203.2307629-1-zhao1.liu@linux.intel.com> MIME-Version: 1.0 From: Zhao Liu Topology index is used to identify the topology child under the same parent topology device. This field corresponds to the topology sub index (e.g., socket-id/ core-id/thread-id) used for addressing. Signed-off-by: Zhao Liu --- hw/core/cpu-topo.c | 77 ++++++++++++++++++++++++++++++++++++++ include/hw/core/cpu-topo.h | 6 +++ 2 files changed, 83 insertions(+) diff --git a/hw/core/cpu-topo.c b/hw/core/cpu-topo.c index 4428b979a5dc..3e0c183388d8 100644 --- a/hw/core/cpu-topo.c +++ b/hw/core/cpu-topo.c @@ -50,6 +50,66 @@ static const char *cpu_topo_level_to_string(CPUTopoLevel level) return NULL; } +static void cpu_topo_refresh_free_child_index(CPUTopoState *topo) +{ + CPUTopoState *child; + + /* + * Fast way: Assume that the index grows sequentially and that there + * are no "index hole" in the previous children. + * + * The previous check on num_children ensures that free_child_index + 1 + * does not hit the max_children limit. + */ + if (topo->free_child_index + 1 == topo->num_children) { + topo->free_child_index++; + return; + } + + /* Slow way: Search the "index hole". The index hole must be found. */ + for (int index = 0; index < topo->num_children; index++) { + bool existed = false; + + QTAILQ_FOREACH(child, &topo->children, sibling) { + if (child->index == index) { + existed = true; + break; + } + } + + if (!existed) { + topo->free_child_index = index; + return; + } + } +} + +static void cpu_topo_validate_index(CPUTopoState *topo, Error **errp) +{ + CPUTopoState *parent = topo->parent, *child; + + if (topo->index < 0) { + error_setg(errp, "Invalid topology index (%d).", + topo->index); + return; + } + + if (parent->max_children && topo->index >= parent->max_children) { + error_setg(errp, "Invalid topology index (%d): " + "The maximum index is %d.", + topo->index, parent->max_children); + return; + } + + QTAILQ_FOREACH(child, &topo->children, sibling) { + if (child->index == topo->index) { + error_setg(errp, "Duplicate topology index (%d)", + topo->index); + return; + } + } +} + static void cpu_topo_build_hierarchy(CPUTopoState *topo, Error **errp) { CPUTopoState *parent = topo->parent; @@ -80,7 +140,18 @@ static void cpu_topo_build_hierarchy(CPUTopoState *topo, Error **errp) } parent->num_children++; + if (topo->index == UNASSIGNED_TOPO_INDEX) { + topo->index = parent->free_child_index; + } else if (topo->index != parent->free_child_index) { + /* The index has been set, then we need to validate it. */ + cpu_topo_validate_index(topo, errp); + if (*errp) { + return; + } + } + QTAILQ_INSERT_TAIL(&parent->children, topo, sibling); + cpu_topo_refresh_free_child_index(parent); } static void cpu_topo_set_parent(CPUTopoState *topo, Error **errp) @@ -135,6 +206,10 @@ static void cpu_topo_destroy_hierarchy(CPUTopoState *topo) QTAILQ_REMOVE(&parent->children, topo, sibling); parent->num_children--; + if (topo->index < parent->free_child_index) { + parent->free_child_index = topo->index; + } + if (!parent->num_children) { parent->child_level = CPU_TOPO_UNKNOWN; } @@ -180,6 +255,8 @@ static void cpu_topo_instance_init(Object *obj) CPUTopoState *topo = CPU_TOPO(obj); QTAILQ_INIT(&topo->children); + topo->index = UNASSIGNED_TOPO_INDEX; + topo->free_child_index = 0; topo->child_level = CPU_TOPO_UNKNOWN; } diff --git a/include/hw/core/cpu-topo.h b/include/hw/core/cpu-topo.h index ebcbdd854da5..c0dfff9dc63b 100644 --- a/include/hw/core/cpu-topo.h +++ b/include/hw/core/cpu-topo.h @@ -24,6 +24,8 @@ #include "hw/qdev-core.h" #include "qemu/queue.h" +#define UNASSIGNED_TOPO_INDEX -1 + typedef enum CPUTopoLevel { CPU_TOPO_UNKNOWN, CPU_TOPO_THREAD, @@ -53,6 +55,8 @@ struct CPUTopoClass { /** * CPUTopoState: + * @index: Topology index within parent's topology queue. + * @free_child_index: Cached free index to be specified for next child. * @num_children: Number of topology children under this topology device. * @max_children: Maximum number of children allowed to be inserted under * this topology device. @@ -66,6 +70,8 @@ struct CPUTopoState { DeviceState parent_obj; /*< public >*/ + int index; + int free_child_index; int num_children; int max_children; CPUTopoLevel child_level;