From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F50B606A32F for ; Tue, 1 Dec 2026 11:22:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=oozQLTq1 Received: from list by lists.xenproject.org with outflank-mailman.1508488.1701414 (Exim 4.92) (envelope-from ) id 1xYLwK-0004dh-6T; Tue, 01 Dec 2026 11:22:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1508488.1701414; Tue, 01 Dec 2026 11:22:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xYLwK-0004da-3U; Tue, 01 Dec 2026 11:22:28 +0000 Received: by outflank-mailman (input) for mailman id 1508488; Tue, 01 Dec 2026 11:22:26 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xYLwI-0004dU-AO for xen-devel@lists.xenproject.org; Tue, 01 Dec 2026 11:22:26 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xYLwH-004V4Z-0n for xen-devel@lists.xenproject.org; Tue, 01 Dec 2026 12:22:25 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b0eadee-2eae-0a2a0a5409dd-0a2a4505bbe2-4 for ; Tue, 01 Dec 2026 12:22:24 +0100 Received: from [52.101.56.15] (helo=BN1PR04CU002.outbound.protection.outlook.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b0eaded-4cb1-0a2a45050019-3465380f6faa-3 for ; Tue, 01 Dec 2026 12:22:23 +0100 Received: from CH2PR20CA0015.namprd20.prod.outlook.com (2603:10b6:610:58::25) by IA0PR12MB8352.namprd12.prod.outlook.com (2603:10b6:208:3dd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.11; Tue, 1 Dec 2026 11:22:16 +0000 Received: from DS2PEPF00006E1F.namprd05.prod.outlook.com (2603:10b6:610:58:cafe::1e) by CH2PR20CA0015.outlook.office365.com (2603:10b6:610:58::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.10 via Frontend Transport; Tue, 1 Dec 2026 11:22:15 +0000 Received: from satlexmb07.amd.com (165.204.84.17) by DS2PEPF00006E1F.mail.protection.outlook.com (10.167.23.212) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Tue, 1 Dec 2026 11:22:15 +0000 Received: from satlexmb07.amd.com (10.181.42.216) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Tue, 1 Dec 2026 05:22:14 -0600 Received: from [192.168.31.141] (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server id 15.2.2562.57 via Frontend Transport; Tue, 1 Dec 2026 05:22:13 -0600 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=amd.com header.i="@amd.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fU4NDeDilWG04U9SnDuCEfi2tkWu8t3SjZCOcNNWAO+SOVQhsfsj1hkAwc1qfvclAxaZujyfUc0znjvm4hJ1yRm18Gr3F2asSALsDsvvoWkS4ejO1Qh4YgI7OPz4ugKzn3I4rd+OCA5HG4u0Z/iqXedpxAm2g+ncFiSzPScjCi0DP4oAMqucoSGlfqtDdYGLGVx7xqO+aiHDZeAL3DOaWyd9vE7wHnGU+v6wOjq+e5rz8UH/JiTNPIToeuSKjqjhNiBG3Rz6Hy73i2DsB+RR7Iu+nltQMtq/JzG3CH84/5m8GwU4uh7EWwNC9+BXLrvG0Equ9HWbKf4lZ/eoOmd/ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UDKWQSU1KDRo3P7axae30+rI89yZbtZaTMDUiok5pUo=; b=MJepbKs1RqgdbBBAWNYHwcfq8UOVlLu+hrktbCK4oUrX23udhJcNjh02bddu/jiHnFhH0cdmuYu4wZbsZ09DXpWA6YUQ1IN+1nKjKUjFak7drZiPbq6uus0NPEv9/Ql8vMpl6BVRUq+JwthIuVlDEpSBONjGq74f5loic5qw5kUU0RmjciIl8m0yg6K5VpbwTBXfrvna2yqlR5WcWijLuzAXaLCRZLnG59/U4bk+KLOtznZ5ziPj8jUu8sOuGHU9vmqlIuPSIg21q/2yee4ab3E+ntxWb8p0xJHiqUhg94VkO30w194OPCeyLF1MvjmKfCENFM5GQ7+jEDJdHqdG1w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=valinux.co.jp smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UDKWQSU1KDRo3P7axae30+rI89yZbtZaTMDUiok5pUo=; b=oozQLTq1Iq67WN2CEMfkvX8dZKeYRYYFLluj2v5et/p6gfdFEESRg9bvayJcfi+7+LUd0X592+/TWxxEw1N0sIcBXYnBHTn7lP/bxDEHbZT3nC/PH0qEC4bqbsAgpaYnBq4TnpyXi1E0R8y5mv7uF1QcarfbAQKoNuRXZB0sVUc= X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Message-ID: Date: Tue, 1 Dec 2026 12:22:12 +0100 X-Archived-By: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v13 1/5] xen/device-tree: Parse 'cpu-map' node for CPU topology exploration To: Hirokazu Takahashi , CC: , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , Anthony PERARD , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= References: <20261130065721.95202-1-taka@valinux.co.jp> <20261130065721.95202-2-taka@valinux.co.jp> From: "Orzel, Michal" Content-Language: en-US In-Reply-To: <20261130065721.95202-2-taka@valinux.co.jp> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00006E1F:EE_|IA0PR12MB8352:EE_ X-MS-Office365-Filtering-Correlation-Id: 3facf9a0-aea8-446d-9e23-08df4f9d5a6a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|82310400026|1800799024|36860700016|23010399003|4143699003|11063799006|6133799003|261009223027099003|18002099003|22082099003|260925021311599003|260925022911599003|260925021911599003|10067099003|56012099006|261028144727099003|261026140227099003|261026121527099003|261022075727099003|261022105027099003|261021121427099003|261021123027099003|261102123227099003; X-Microsoft-Antispam-Message-Info: xLG6syFb7sqMM0t5NOFuy+CWdSKF62aiGQXz+wohGplkja4M5JenCVkhpjLrRJt+HXPbNzRnylwsdkI3AetBrJ/SXGWG5DF0x//Tp2xWptqtDyl0VTqTlbqR/65kT788AOfcFR9YHLa3mZu2P7gocfn7VjAeZuEje+3t6lU/BcRhv/9BRFXv86Rfff4mtdds0MxAhkwpdfO8Qn5urUM1/UrAckdnTzarzSymlbcxklVBYa7LRmDGy5IH41RSxfafpc314Fo0dTBvIC9/hoH8tsw5AyAPkgPJ3AGbH012FtRpRtm4/O8JJrdOf0yhldI1Gyi+e2hHGzehhl5YfMBUqECLzEx8HF/Dy0h33mYyDwWnmMJX7B1ZT9gAtLRCGGW1RKwooFBolF1hpLBc495Pa2RLy+CFVr5TW9qg0LDNpBRgsdRJxwDITQFAAFgtwmur5MyKjZ6ZvykiULmrN/WFhfmVwxq4ZtvQiyn0KBnTjt5tqRcLG5RFBwCIy6xNIDzBr5AZ9maQ7ZeiuTcwfRbWECjDRam8s4gse4V6XVvDz9MLlVSBq1aCZGiH5gx7b5TugrxXvQ9BsOeNlZA8aZqZXqSfd3H4Ib2e6D6QUfeTVdtIBUf4U5uOpSQVcqLcE7OR3GdayESZJyO5t6yjPcqALRLMLYyLrHMCMWO/DstNhurCWcvDSjrZ2sleFBG703gdjufdyL4iEOgWafDNx9MFWQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(7416014)(82310400026)(1800799024)(36860700016)(23010399003)(4143699003)(11063799006)(6133799003)(261009223027099003)(18002099003)(22082099003)(260925021311599003)(260925022911599003)(260925021911599003)(10067099003)(56012099006)(261028144727099003)(261026140227099003)(261026121527099003)(261022075727099003)(261022105027099003)(261021121427099003)(261021123027099003)(261102123227099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ikkUzPguTpsV0OAykOs6wDYHDxrzUBtTuVW4qJLsuKcOHTcuuERt6UtFYEEqIKSn2oYzK2D9VzSkZO/IpLp/HshWoTgsr73O/kXTCHMeNIXTLv+jzIWX19+qqo5bRy9zsQTX6JGhUMg/IRr4anGCkd4F1zAZ2h5/Dl2Qbn34zLMfIZJx+qjPCN3kIL0SUR+759rqK5xkDNeYfAc8WHEMtvLpWdrenzxQ3G2uUC7bY9U3cBdKojHqutZYIl3HoHzMgrNtvdHWXGfuSrQoxkgydJ+Npcd9Cdbopr7q38DuHdEZox7T+YHn9tgXeWivWjnAY5vzwSflKV5OQoUjshjRac/YBbAXUIXAkq/yGFMhgzB3EkO4L2G9jZriE+WMzMNxbQYxaqyvTfkpVcyyhx0ohICwKy0VHEFLE3j4Fh2Rb1DoK9LIiBOOOWBw01rf3OOo X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 11:22:15.4491 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3facf9a0-aea8-446d-9e23-08df4f9d5a6a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF00006E1F.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8352 X-purgate-ID: tlsNG-c201ff/1796124144-6BEB92A1-C648B14D/0/0 X-purgate-type: clean X-purgate-size: 21011 X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 30-Nov-26 07:57, Hirokazu Takahashi wrote: > Parse the 'cpu-map' node in the Device Tree to extract CPU topology > information. If the 'cpu-map' node is absent, simply ignore it. For the v14, that hopefully will be the last revision of this patch, can you please extend the commit msg. For such a big change it deserves a proper commit msg explaining what, why and how. > > Signed-off-by: Hirokazu Takahashi > --- > Changes in v13: > - Rename drivers/acpi/topology.c to drivers/acpi/cpu-topology.c. > - Exit the loop immediately when memory allocation fails. > - Avoid printing redundant error messages on -ENOMEM. > - Do not log error messages on -ENOENT or -ENOTSUPP. > - Add appropriate XENLOG levels to printk() calls that lacked them. > - Replace dt_node_name() with dt_node_full_name() in error logs to > provide more detailed information. > > xen/arch/arm/Kconfig | 1 + > xen/arch/arm/setup.c | 3 + > xen/arch/arm/smpboot.c | 10 + > xen/common/Kconfig | 22 ++ > xen/common/Makefile | 1 + > xen/common/cpu-topology.c | 81 ++++++ > xen/common/cpu.c | 5 + > xen/common/device-tree/Makefile | 1 + > xen/common/device-tree/cpu-topology.c | 374 ++++++++++++++++++++++++++ > xen/drivers/acpi/Makefile | 1 + > xen/drivers/acpi/cpu-topology.c | 43 +++ > xen/include/xen/acpi.h | 13 + > xen/include/xen/cpu-topology.h | 34 +++ > xen/include/xen/dt-cpu-topology.h | 35 +++ > 14 files changed, 624 insertions(+) > create mode 100644 xen/common/cpu-topology.c > create mode 100644 xen/common/device-tree/cpu-topology.c > create mode 100644 xen/drivers/acpi/cpu-topology.c > create mode 100644 xen/include/xen/cpu-topology.h > create mode 100644 xen/include/xen/dt-cpu-topology.h > > diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig > index b9c5c40678..3d0f689220 100644 > --- a/xen/arch/arm/Kconfig > +++ b/xen/arch/arm/Kconfig > @@ -19,6 +19,7 @@ config ARM > select HAS_ALTERNATIVE if HAS_VMAP > select HAS_DEVICE_TREE_DISCOVERY > select HAS_DOM0LESS > + select HAS_GENERIC_CPU_TOPOLOGY > select HAS_GRANT_CACHE_FLUSH if GRANT_TABLE > select HAS_SHARED_INFO > select HAS_STACK_PROTECTOR > diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c > index 2cd1c021b5..c1aa06bd31 100644 > --- a/xen/arch/arm/setup.c > +++ b/xen/arch/arm/setup.c > @@ -10,6 +10,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -375,6 +376,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr) > nr_cpu_ids = smp_get_max_cpus(); > printk(XENLOG_INFO "SMP: Allowing %u CPUs\n", nr_cpu_ids); > > + init_cpu_topology(); > + > /* > * Some errata relies on SMCCC version which is detected by psci_init() > * (called from smp_init_cpus()). > diff --git a/xen/arch/arm/smpboot.c b/xen/arch/arm/smpboot.c > index 2fc6c36ca1..97e227cd92 100644 > --- a/xen/arch/arm/smpboot.c > +++ b/xen/arch/arm/smpboot.c > @@ -9,10 +9,12 @@ > > #include > #include > +#include > #include > #include > #include > #include > +#include > #include > #include > #include > @@ -268,6 +270,14 @@ static void __init dt_smp_init_cpus(void) > } > else > tmp_map[i] = hwid; > + > + /* > + * Because the boot CPU (i == 0) is already running, treat it as valid > + * even if arch_cpu_init() fails (e.g. due to a missing enable-method). > + */ > + if ( (rc >= 0) || (i == 0) ) > + /* Pass the info to dt_init_cpu_topology() */ > + map_cpu_to_dt_node(i, cpu); > } > > if ( !bootcpu_valid ) > diff --git a/xen/common/Kconfig b/xen/common/Kconfig > index a619f2cef1..637ea0d486 100644 > --- a/xen/common/Kconfig > +++ b/xen/common/Kconfig > @@ -143,6 +143,9 @@ config HAS_EX_TABLE > config HAS_FAST_MULTIPLY > bool > > +config HAS_GENERIC_CPU_TOPOLOGY > + bool > + > config HAS_IOPORTS > bool > > @@ -197,6 +200,25 @@ config VM_EVENT > config NEEDS_LIBELF > bool > > +config GENERIC_CPU_TOPOLOGY > + bool > + > +config DT_CPU_TOPOLOGY > + bool "Device tree based CPU topology support" > + depends on HAS_GENERIC_CPU_TOPOLOGY && DEVICE_TREE_PARSE > + select GENERIC_CPU_TOPOLOGY > + help > + Retrieve CPU topology information from the device tree to optimize > + vCPU scheduling. > + > +config ACPI_CPU_TOPOLOGY > + bool "ACPI based CPU topology support" > + depends on HAS_GENERIC_CPU_TOPOLOGY && ACPI > + select GENERIC_CPU_TOPOLOGY > + help > + Retrieve CPU topology information from the ACPI PPTT to optimize > + vCPU scheduling. > + > config NUMA > bool > > diff --git a/xen/common/Makefile b/xen/common/Makefile > index cbabaf1e2b..25a9feef10 100644 > --- a/xen/common/Makefile > +++ b/xen/common/Makefile > @@ -6,6 +6,7 @@ obj-$(CONFIG_GENERIC_BUG_FRAME) += bug.o > obj-$(CONFIG_HYPFS_CONFIG) += config_data.o > obj-$(CONFIG_CORE_PARKING) += core_parking.o > obj-y += cpu.o > +obj-$(CONFIG_GENERIC_CPU_TOPOLOGY) += cpu-topology.init.o > obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o > obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += device.o > obj-$(filter-out $(CONFIG_X86),$(CONFIG_ACPI)) += device.o > diff --git a/xen/common/cpu-topology.c b/xen/common/cpu-topology.c > new file mode 100644 > index 0000000000..e9275924ca > --- /dev/null > +++ b/xen/common/cpu-topology.c > @@ -0,0 +1,81 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +static void __init free_topology_table(void) > +{ > + unsigned int cpu; > + > + if ( !cpu_topology ) > + return; You did not use goto as I suggested and instead used ifs. It's ok but with that this check is now dead. > + > + for ( cpu = 0; cpu < nr_cpu_ids; cpu++ ) > + { > + free_cpumask_var(cpu_topology[cpu].thread_sibling); > + free_cpumask_var(cpu_topology[cpu].core_sibling); > + free_cpumask_var(cpu_topology[cpu].cluster_sibling); > + } > + > + XVFREE(cpu_topology); > +} > + > +void __init init_cpu_topology(void) > +{ > + unsigned int cpu; > + int ret; > + > + if ( num_possible_cpus() == 1 ) > + return; > + > + cpu_topology = xvzalloc_array(struct cpu_topology, nr_cpu_ids); > + > + if ( cpu_topology ) > + { > + for ( cpu = 0; cpu < nr_cpu_ids; cpu++ ) > + { > + if ( !zalloc_cpumask_var(&cpu_topology[cpu].thread_sibling) || > + !zalloc_cpumask_var(&cpu_topology[cpu].core_sibling) || > + !zalloc_cpumask_var(&cpu_topology[cpu].cluster_sibling) ) > + { > + free_topology_table(); > + break; > + } > + } > + } > + > + if ( !cpu_topology ) > + { > + printk(XENLOG_WARNING "CPU topology table allocation failed\n"); > + return; > + } > + > + if ( acpi_disabled ) > + ret = dt_init_cpu_topology(); > + else > + ret = acpi_init_cpu_topology(); > + > + /* Free the CPU topology table if initialization fails. */ > + if ( ret != 0 ) > + { > + free_topology_table(); > + > + if ( (ret != -ENOENT) && (ret != -EOPNOTSUPP) ) > + printk(XENLOG_WARNING > + "Failed to initialize cpu topology (error %d)\n", ret); > + } > +} > + > +/* > + * Local variables: > + * mode: C > + * c-file-style: "BSD" > + * c-basic-offset: 4 > + * tab-width: 4 > + * indent-tabs-mode: nil > + * End: > + */ > diff --git a/xen/common/cpu.c b/xen/common/cpu.c > index f09af0444b..9591ada60a 100644 > --- a/xen/common/cpu.c > +++ b/xen/common/cpu.c > @@ -1,5 +1,6 @@ > #include > #include > +#include > #include > #include > #include > @@ -46,6 +47,10 @@ const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { > #undef MASK_DECLARE_2 > #undef MASK_DECLARE_1 > > +#ifdef CONFIG_GENERIC_CPU_TOPOLOGY > +struct cpu_topology *__ro_after_init cpu_topology; > +#endif /* CONFIG_GENERIC_CPU_TOPOLOGY */ > + > static DEFINE_RWLOCK(cpu_add_remove_lock); > > bool get_cpu_maps(void) > diff --git a/xen/common/device-tree/Makefile b/xen/common/device-tree/Makefile > index 730d202349..b15b3382d2 100644 > --- a/xen/common/device-tree/Makefile > +++ b/xen/common/device-tree/Makefile > @@ -1,6 +1,7 @@ > obj-y += bootfdt.init.o > obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo-fdt.init.o > obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo.init.o > +obj-$(CONFIG_DT_CPU_TOPOLOGY) += cpu-topology.init.o > obj-y += device-tree.o > obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o > obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o > diff --git a/xen/common/device-tree/cpu-topology.c b/xen/common/device-tree/cpu-topology.c > new file mode 100644 > index 0000000000..0561b4856d > --- /dev/null > +++ b/xen/common/device-tree/cpu-topology.c > @@ -0,0 +1,374 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * Derived from Linux kernel 7.0's $drivers/base/arch_topology.c > + * Parse cpu topology information. > + */ > + > +#include xen/acpi.h is not used here and please include xen/dt-cpu-topology.h > +#include > +#include > +#include > +#include > +#include > + > +#define INVALID_TOPO_ID (~0U) > + > +struct cpu_map { > + unsigned int thread_id; > + unsigned int core_id; > + unsigned int cluster_id; > + unsigned int socket_id; > +}; > + > +static struct cpu_map __initdata cpu_map[NR_CPUS] = { > + [0 ... NR_CPUS - 1] = { > + .thread_id = INVALID_TOPO_ID, > + .core_id = INVALID_TOPO_ID, > + .cluster_id = INVALID_TOPO_ID, > + .socket_id = INVALID_TOPO_ID, > + }, > +}; > +static struct dt_device_node *__initdata dt_cpu_table[NR_CPUS]; > + > +static void __init setup_siblings_masks(unsigned int target_cpu) > +{ > + struct cpu_topology *target_topo = &cpu_topology[target_cpu]; > + const struct cpu_map *target_map = &cpu_map[target_cpu]; > + unsigned int cpu; > + > + /* Update cluster, core and thread sibling masks */ > + for_each_possible_cpu(cpu) > + { > + struct cpu_topology *cpu_topo = &cpu_topology[cpu]; > + const struct cpu_map *map = &cpu_map[cpu]; > + > + if ( target_cpu > cpu ) > + continue; > + > + if ( target_map->socket_id != map->socket_id ) > + continue; > + > + cpumask_set_cpu(target_cpu, cpu_topo->core_sibling); > + cpumask_set_cpu(cpu, target_topo->core_sibling); > + > + if ( target_map->cluster_id != map->cluster_id ) > + continue; > + > + cpumask_set_cpu(target_cpu, cpu_topo->cluster_sibling); > + cpumask_set_cpu(cpu, target_topo->cluster_sibling); > + > + if ( target_map->core_id != map->core_id ) > + continue; > + > + cpumask_set_cpu(target_cpu, cpu_topo->thread_sibling); > + cpumask_set_cpu(cpu, target_topo->thread_sibling); > + } > +} > + > +static const struct dt_device_node *__init dt_find_child_node_by_name( > + const struct dt_device_node *dt, > + const char *name) > +{ > + const struct dt_device_node *np; > + > + dt_for_each_child_node(dt, np) > + if ( np->name && dt_node_name_is_equal(np, name) ) > + return np; > + > + return NULL; > +} > + > +void __init map_cpu_to_dt_node(unsigned int cpu, > + struct dt_device_node *cpu_node) > +{ > + if ( cpu < ARRAY_SIZE(dt_cpu_table) ) > + dt_cpu_table[cpu] = cpu_node; > + else > + printk(XENLOG_WARNING > + "cpu %u exceeds the max cpus %zu\n", > + cpu, ARRAY_SIZE(dt_cpu_table)); > +} > + > +static unsigned int __init cpu_node_to_id( > + const struct dt_device_node *cpu_node) > +{ > + unsigned int cpu; > + > + for_each_possible_cpu(cpu) > + if ( cpu_node == dt_cpu_table[cpu] ) > + return cpu; > + > + return INVALID_TOPO_ID; > +} > + > +/* > + * This function returns the Xen cpu number of the DT node. > + */ > +static int __init get_cpu_for_node( > + const struct dt_device_node *dt_node, unsigned int *cpu) > +{ > + const struct dt_device_node *cpu_node = > + dt_parse_phandle(dt_node, "cpu", 0); > + > + if ( !cpu_node || !dt_device_type_is_equal(cpu_node, "cpu") ) > + { > + *cpu = INVALID_TOPO_ID; > + return -EINVAL; > + } > + > + *cpu = cpu_node_to_id(cpu_node); > + > + return 0; > +} > + > +static bool __init is_cpu_map_empty(unsigned int cpu) > +{ > + return (cpu_map[cpu].socket_id == INVALID_TOPO_ID) && > + (cpu_map[cpu].cluster_id == INVALID_TOPO_ID) && > + (cpu_map[cpu].core_id == INVALID_TOPO_ID) && > + (cpu_map[cpu].thread_id == INVALID_TOPO_ID); > +} > + > +static int __init parse_core(const struct dt_device_node *core, > + unsigned int socket_id, > + unsigned int cluster_id, > + unsigned int core_id) > +{ > + bool leaf = true; > + unsigned int thread_id; > + unsigned int cpu; > + int ret; > + > + for ( thread_id = 0; ; thread_id++ ) > + { > + const struct dt_device_node *thread; > + char name[20]; > + > + snprintf(name, sizeof(name), "thread%u", thread_id); > + thread = dt_find_child_node_by_name(core, name); > + > + if ( !thread ) > + break; > + > + leaf = false; > + ret = get_cpu_for_node(thread, &cpu); > + if ( ret ) > + { > + printk(XENLOG_ERR > + "%s: No CPU found for thread\n", dt_node_full_name(thread)); > + return ret; > + } > + > + if ( cpu == INVALID_TOPO_ID ) > + { > + /* > + * Ignore this CPU node definition as the corresponding physical > + * CPU is unavailable. > + */ > + printk(XENLOG_INFO > + "%s: CPU for thread is unavailable\n", > + dt_node_full_name(thread)); > + continue; > + } > + > + if ( !is_cpu_map_empty(cpu) ) > + { > + printk(XENLOG_ERR > + "Duplicate CPU definition for CPU%u\n", cpu); > + return -EINVAL; > + } > + > + cpu_map[cpu].socket_id = socket_id; > + cpu_map[cpu].cluster_id = cluster_id; > + cpu_map[cpu].core_id = core_id; > + cpu_map[cpu].thread_id = thread_id; > + } > + > + ret = get_cpu_for_node(core, &cpu); > + > + if ( cpu != INVALID_TOPO_ID ) > + { > + if ( !leaf ) > + { > + printk(XENLOG_ERR "%s: Core has both threads and CPU\n", > + dt_node_full_name(core)); > + return -EINVAL; > + } > + > + if ( !is_cpu_map_empty(cpu) ) > + { > + printk(XENLOG_ERR "Duplicate CPU definition for CPU%u\n", cpu); > + return -EINVAL; > + } > + > + cpu_map[cpu].socket_id = socket_id; > + cpu_map[cpu].cluster_id = cluster_id; > + cpu_map[cpu].core_id = core_id; > + cpu_map[cpu].thread_id = 0; > + } > + else if ( leaf ) > + { > + if ( ret ) > + { > + printk(XENLOG_ERR > + "%s: No CPU found for leaf core\n", > + dt_node_full_name(core)); > + return ret; > + } > + > + /* > + * Ignore this CPU node definition as the corresponding physical > + * CPU is unavailable. > + */ > + printk(XENLOG_INFO > + "%s: CPU for leaf core is unavailable\n", > + dt_node_full_name(core)); > + } > + > + return 0; > +} > + > +static int __init parse_cluster(const struct dt_device_node *cluster, > + unsigned int socket_id, > + unsigned int cluster_id) > +{ > + bool has_cores = false; > + int ret = 0; > + > + if ( dt_find_child_node_by_name(cluster, "cluster0") ) > + { > + printk(XENLOG_ERR > + "Topology for clusters of clusters not yet supported\n"); > + return -EINVAL; > + } > + > + for ( unsigned int core_id = 0; ; core_id++ ) > + { > + const struct dt_device_node *core; > + char name[20]; > + > + snprintf(name, sizeof(name), "core%u", core_id); > + core = dt_find_child_node_by_name(cluster, name); > + > + if ( !core ) > + break; > + > + has_cores = true; > + > + ret = parse_core(core, socket_id, cluster_id, core_id); > + if ( ret != 0 ) > + return ret; > + } > + > + if ( !has_cores ) > + printk(XENLOG_WARNING "%s: empty cluster\n", > + dt_node_full_name(cluster)); Incorrect indentation. > + > + return ret; > +} > + > +static int __init parse_socket(const struct dt_device_node *socket, > + unsigned int socket_id) > +{ > + bool has_cluster = false; > + int ret = 0; > + > + for ( unsigned int cluster_id = 0; ; cluster_id++ ) > + { > + const struct dt_device_node *cluster; > + char name[20]; > + > + snprintf(name, sizeof(name), "cluster%u", cluster_id); > + cluster = dt_find_child_node_by_name(socket, name); > + > + if ( !cluster ) > + break; > + > + has_cluster = true; > + ret = parse_cluster(cluster, socket_id, cluster_id); > + if ( ret != 0 ) > + return ret; > + } > + > + /* > + * If no cluster node is defined, assume the socket has > + * a single cluster. > + */ > + if ( !has_cluster ) > + ret = parse_cluster(socket, socket_id, 0); > + > + return ret; > +} > + > +static int __init parse_package(const struct dt_device_node *package) > +{ > + bool has_socket = false; > + int ret = 0; > + > + for ( unsigned int socket_id = 0; ; socket_id++ ) > + { > + const struct dt_device_node *socket; > + char name[20]; > + > + snprintf(name, sizeof(name), "socket%u", socket_id); > + socket = dt_find_child_node_by_name(package, name); > + > + if ( !socket ) > + break; > + > + has_socket = true; > + ret = parse_socket(socket, socket_id); > + if ( ret != 0 ) > + return ret; > + } > + > + /* > + * If no socket node is defined, assume all clusters reside under > + * a single socket. > + */ > + if ( !has_socket ) > + ret = parse_socket(package, 0); > + > + return ret; > +} > + > +static int __init parse_dt_topology(void) > +{ > + const struct dt_device_node *cpus; > + const struct dt_device_node *map; > + > + cpus = dt_find_node_by_path("/cpus"); > + if ( !cpus ) > + return -ENOENT; > + > + map = dt_find_child_node_by_name(cpus, "cpu-map"); > + if ( !map ) > + return -ENOENT; > + > + return parse_package(map); > +} > + > +int __init dt_init_cpu_topology(void) > +{ > + unsigned int cpu; > + int ret; > + > + ASSERT(cpu_topology); > + > + ret = parse_dt_topology(); > + if ( ret == 0 ) > + for_each_possible_cpu(cpu) > + setup_siblings_masks(cpu); I thought about what I told you a few revisions ago about a missing check if we compare this code to Linux. I think we need to have one i.e. to check that every CPU is indeed described in our cpu topology. Otherwise, if `cpu-map` does not describe a CPU, such CPU keeps the invalid values and ends up being placed in its own socket/cluster/core. ~Michal