From patchwork Tue Feb 13 21:04:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 200596 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp807132dyb; Tue, 13 Feb 2024 13:04:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUnx3MOAtN2JRTYT18zp76YFUY1+SA2Z0ULp92nNhpJ23Ga9KckNGEip6ZpslHqj0uevFf0Tf1tYvknTdXor8xM0YQ9RQ== X-Google-Smtp-Source: AGHT+IHxeklxuNxEkv1LCkLcS/RetTNn8z7noIq4g+VvtI8zc2pwotFfhX9U80gbNiqJLV4kQFKw X-Received: by 2002:a05:6102:3199:b0:46d:6b94:8b81 with SMTP id c25-20020a056102319900b0046d6b948b81mr579454vsh.24.1707858295901; Tue, 13 Feb 2024 13:04:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707858295; cv=pass; d=google.com; s=arc-20160816; b=q+KrGOR/eBx2O5fEcPj6v5FOAJJTksm2VJNdnbkaaQrMYRc/qIJzT6MfSPEzoJOIel 1uLwTiVq7kTqyDsS6cWeZGomgQEQe4/D+DmpBRwMjFvoluBfRxJAlMTurTVawCQk4jMb 5xHs624jhJ5Hs7Ojxh1eRlb4PN57Odn9RUcO5qBN292ixEyc4rB869hdmDEUXKSPOtjv uaArbF60SFxhov8SBZuR2Wg1rzy1mu0GpV4xj302mi5K+WtdJj2XVJNVIm9bLBG5xgjF AkwXdKdGf536htSa4VZEXGkOZynD9deDYZ2Rr0nKxvMiyvCzxGv+2a+te2jJtQuG3xaW E62g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=boqcIU/ZFA+rm7+t4NycyG5KGsKb0/dLPE34Do9xfZg=; fh=5kW9mUZv91yqEO6h7HSpzrt0En9nYnZYCHh3HiZOH4o=; b=PFD0ve0Z3MG2lJFTtbWvddlpjFKJkOaZ0dO1ygRJSfClehttXfTrHiJtpcHRjZYNmo 1A4AdhhaeegULejz6dOD8Y3XmO+AW5o8JZJ3UyUQjxPj9Si8AF6Kw/Qe3OKPcrsNmskW 7CVTmR77kSwtMP4WWxzHEqKhlsX0Xnv5KQhJDTeVaQhSUTW60wAMXbWG+E7NNLbAxtuJ Angn4qw2HZdjwv5PBtV5e2LizFIjLKrMJKLyWD1N16ff5MeIc/BRAULfy0AP9cb7ROvC uDbooapNWk9O9813F74TTFTLw1IqRHCwRLwOKY87LDNRtih7iuuiVV2AO4kXrdMGsigG Nuiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=jK3XKW8T; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64242-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64242-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=2; AJvYcCUCVwnDweXKiAxFZalqW3Pe1xnhGpahgEbz95dE9+wcvAjKwjJ2uJwzDi1Ddx+QQzD7IhwpXkszyUf8K5hw7Rbqvqo9cQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id hv16-20020a67e690000000b0046d35855349si1423576vsb.734.2024.02.13.13.04.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:04:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64242-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=jK3XKW8T; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64242-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64242-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9E3641C2325C for ; Tue, 13 Feb 2024 21:04:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9DD3626A0; Tue, 13 Feb 2024 21:04:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jK3XKW8T"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="a0DvwaJw" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 385F76167E for ; Tue, 13 Feb 2024 21:04:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858247; cv=none; b=k0Xyr1o+Hj1FgDFCNRltSMXZ+Q8EUWZbTrL7hVNvH008RWOnqb5k8SAMlv09ucLy2z6woto16fK4868nenXb/30pOO3zXf/MWeQmN1oxZCkzqcdx8FjS9FzhG3ytgrDHCRFCG1JimMYDqjBWtRDWb0h+N3+TldCG0QZAMX89vXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858247; c=relaxed/simple; bh=rzJRDd07DQzyOI3vFvLyo30rvQuYP+fyKgjiQvWo65A=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tGSpDa9Qe8m5I7uskMCNVvRXs+TyEcUm8K1uC95pUN/m+BbnNds4xYvhCfjKZQGUnNLMkMzFdo7ixrKslbtY89PaYkqv2c1RnuIc+mFOZ+COzOAmip301xnUTSz5o0OumcMoc7IxqmpoU4PIadw1yGyKDVYz4Cb6MYU/VYMBaeo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jK3XKW8T; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=a0DvwaJw; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240212153624.644448852@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1707858243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=boqcIU/ZFA+rm7+t4NycyG5KGsKb0/dLPE34Do9xfZg=; b=jK3XKW8TH25srFoYj/y65xYLR8Ipwzik8HHdyV4FvFFnpDuU27buTTzxYapxo2mlR0fO7L QA01tnBnlJnzauZZBgM4MjBk3G81W0NJiQY2jmdTXMTmwPkq5krFaEpCQPGVlSrHsoQ5Cj TiijkAifIkpJCZsKi/HEdYk/E01DEINB+cf+Eq/uz753X8o+5qdJ88Nec6rZ1i9f0trZf1 G5sTTayOlvbAQdlg7ePxFKG8nwJyxMwvaqv4NKXBJBKl7HEH+th41V5wYqMN17nUK1YxOV kW1efMnb4ztRytnGwtXaFRLzdIWpFSRX9Moov2oAyrfrhF1p0+Ssn30F72yy4g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1707858243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=boqcIU/ZFA+rm7+t4NycyG5KGsKb0/dLPE34Do9xfZg=; b=a0DvwaJwbINSMnYLNG6pS9SuY/Oa/v1OZEKzYPuFUex6iuggsbMIZGrN4bjIfg6FMe2DlG yU28GkI16MYr64AA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Tom Lendacky , Andrew Cooper , Arjan van de Ven , Huang Rui , Juergen Gross , Dimitri Sivanich , Sohil Mehta , K Prateek Nayak , Kan Liang , Zhang Rui , "Paul E. McKenney" , Feng Tang , Andy Shevchenko , Michael Kelley , "Peter Zijlstra (Intel)" , Wang Wendy Subject: [patch V6 03/19] x86/cpu: Add legacy topology parser References: <20240212153109.330805450@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 Feb 2024 22:04:03 +0100 (CET) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790819220751465361 X-GMAIL-MSGID: 1790819220751465361 From: Thomas Gleixner The legacy topology detection via CPUID leaf 4, which provides the number of cores in the package and CPUID leaf 1 which provides the number of logical CPUs in case that FEATURE_HT is enabled and the CMP_LEGACY feature is not set, is shared for Intel, Centaur and Zhaoxin CPUs. Lift the code from common.c without the early detection hack and provide it as common fallback mechanism. Will be utilized in later changes. Signed-off-by: Thomas Gleixner Tested-by: Juergen Gross Tested-by: Sohil Mehta Tested-by: Michael Kelley Tested-by: Zhang Rui Tested-by: Wang Wendy --- V6: Rename parse_num_cores() to parse_num_cores_legacy() - Arjan Change comment style - Borislav --- arch/x86/kernel/cpu/common.c | 3 ++ arch/x86/kernel/cpu/topology.h | 3 ++ arch/x86/kernel/cpu/topology_common.c | 46 +++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) --- --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -892,6 +892,9 @@ void detect_ht(struct cpuinfo_x86 *c) #ifdef CONFIG_SMP int index_msb, core_bits; + if (topo_is_converted(c)) + return; + if (detect_ht_early(c) < 0) return; --- a/arch/x86/kernel/cpu/topology.h +++ b/arch/x86/kernel/cpu/topology.h @@ -6,6 +6,9 @@ struct topo_scan { struct cpuinfo_x86 *c; unsigned int dom_shifts[TOPO_MAX_DOMAIN]; unsigned int dom_ncpus[TOPO_MAX_DOMAIN]; + + /* Legacy CPUID[1]:EBX[23:16] number of logical processors */ + unsigned int ebx1_nproc_shift; }; bool topo_is_converted(struct cpuinfo_x86 *c); --- a/arch/x86/kernel/cpu/topology_common.c +++ b/arch/x86/kernel/cpu/topology_common.c @@ -24,6 +24,48 @@ void topology_set_dom(struct topo_scan * } } +static unsigned int __maybe_unused parse_num_cores_legacy(struct cpuinfo_x86 *c) +{ + struct { + u32 cache_type : 5, + unused : 21, + ncores : 6; + } eax; + + if (c->cpuid_level < 4) + return 1; + + cpuid_subleaf_reg(4, 0, CPUID_EAX, &eax); + if (!eax.cache_type) + return 1; + + return eax.ncores + 1; +} + +static void __maybe_unused parse_legacy(struct topo_scan *tscan) +{ + unsigned int cores, core_shift, smt_shift = 0; + struct cpuinfo_x86 *c = tscan->c; + + cores = parse_num_cores_legacy(c); + core_shift = get_count_order(cores); + + if (cpu_has(c, X86_FEATURE_HT)) { + if (!WARN_ON_ONCE(tscan->ebx1_nproc_shift < core_shift)) + smt_shift = tscan->ebx1_nproc_shift - core_shift; + /* + * The parser expects leaf 0xb/0x1f format, which means + * the number of logical processors at core level is + * counting threads. + */ + core_shift += smt_shift; + cores <<= smt_shift; + } + + topology_set_dom(tscan, TOPO_SMT_DOMAIN, smt_shift, 1U << smt_shift); + topology_set_dom(tscan, TOPO_CORE_DOMAIN, core_shift, cores); +} + bool topo_is_converted(struct cpuinfo_x86 *c) { /* Temporary until everything is converted over. */ @@ -47,7 +89,7 @@ static bool fake_topology(struct topo_sc * which has useless CPUID information. */ topology_set_dom(tscan, TOPO_SMT_DOMAIN, 0, 1); - topology_set_dom(tscan, TOPO_CORE_DOMAIN, 1, 1); + topology_set_dom(tscan, TOPO_CORE_DOMAIN, 0, 1); return tscan->c->cpuid_level < 1 || xen_pv_domain(); } @@ -88,6 +130,8 @@ static void parse_topology(struct topo_s /* The above is sufficient for UP */ if (!IS_ENABLED(CONFIG_SMP)) return; + + tscan->ebx1_nproc_shift = get_count_order(ebx.nproc); } static void topo_set_ids(struct topo_scan *tscan)