From nobody Wed Dec 17 05:28:09 2025 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 2DAD62566CB for ; Wed, 19 Mar 2025 12:21:49 +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=1742386911; cv=none; b=txb7TkD3LKc3R+Ve2h2U14/6VwzJ2SpAQmQNvkHkqR9NGXh7G1d95AV1Ch5ZVJsbI3l13VVqNW+1CJuy0tFIx5NmQwNB1l7rnkrW5R6xMOto9AZQyqDOdBb3i0JLSV6TRm3aWtfM+CLXmnhUd/x9DS45jNikFdcvBdNpHHx/G3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386911; c=relaxed/simple; bh=tjwaLaLIE24h6R3231TizZjukZ/rW3xkDxKWRUfWlc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MzTHHT7oIAP3B1NvamMmyF2NvQbe7pDWVUYUD4pPIQIgZQLBCMjN/xOf3WksgC2o5njKQ4kBrYeipfGu4q5vPAcGBp4kqDJvptWmESTi1ImtxnBCZnxin4MiLzYPwknQN/NKd6MoKL9OtMJof2gHMIJA5uFdFASWUKbdWV5T0hs= 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=GAGsFU3m; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Hl70R5hh; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GAGsFU3m"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Hl70R5hh" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m6bUDRKa3h7q/AVQQzk6Gc5gVSuHQk5hT+ivElcSG98=; b=GAGsFU3mMoCcNcahlJBwfIoKBtl3ADHDgsZEZFT4KIxhkhJ2RLO7Ri69hLu1SYq7yNIpdX 2QJyg/GRuAD6S17wBKkDKINq1v4UWspiJGKgsuJ3KjD5MflEv9XjE9+/TDhPDDpc1jPuFK OBidxc2py0EPetUSO9dYT+6RgV3TOKANHSxCsX4OwkzM/wN9jZLyZ9o20SbJQ6yebApb8B 9aM+3KiMEy2ZZqbLfAsHOKorW7Bn3Ih9wWFGPnTNFsP20AkMQY5Akrr/cbnwyfsM2BIDQ8 yZvWf4TmdOy1nP004PEnTrjpHrsRyDh1/XEkgcQu6jJXsrgO3Qwhv59l/4kuFA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m6bUDRKa3h7q/AVQQzk6Gc5gVSuHQk5hT+ivElcSG98=; b=Hl70R5hhNIiM5n1FVLokX7KeHuYj8oW1KziLCy3hsKG5tSGynEU4oEPo3Z3BR8XyZUH4CW yG4fLWxML1wmHuCg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 01/29] x86/cpu: Remove leaf 0x2 parsing loop Date: Wed, 19 Mar 2025 13:21:09 +0100 Message-ID: <20250319122137.4004-2-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Leaf 0x2 output includes a "query count" byte where it was supposed to specify the number of repeated CPUID leaf 0x2 subleaf 0 queries needed to extract all of the CPU's cache and TLB descriptors. Per current Intel manuals, all CPUs supporting this leaf "will always" return an iteration count of 1. Remove the leaf 0x2 query loop and just query the hardware once. Note, as previously done at commit aec28d852ed2 ("x86/cpuid: Standardize on u32 in "), standardize on using 'u32' and 'u8' types. Suggested-by: Ingo Molnar Suggested-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/intel.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 4cbb2e69bea1..0570d4d86006 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -6,6 +6,7 @@ #include #include #include +#include =20 #ifdef CONFIG_X86_64 #include @@ -777,28 +778,27 @@ static void intel_tlb_lookup(const unsigned char desc) =20 static void intel_detect_tlb(struct cpuinfo_x86 *c) { - int i, j, n; - unsigned int regs[4]; - unsigned char *desc =3D (unsigned char *)regs; + u32 regs[4]; + u8 *desc =3D (u8 *)regs; =20 if (c->cpuid_level < 2) return; =20 - /* Number of times to iterate */ - n =3D cpuid_eax(2) & 0xFF; + cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); =20 - for (i =3D 0 ; i < n ; i++) { - cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); - - /* If bit 31 is set, this is an unknown format */ - for (j =3D 0 ; j < 4 ; j++) - if (regs[j] & (1 << 31)) - regs[j] =3D 0; + /* Intel CPUs must report an iteration count of 1 */ + if (desc[0] !=3D 0x01) + return; =20 - /* Byte 0 is level count, not a descriptor */ - for (j =3D 1 ; j < 16 ; j++) - intel_tlb_lookup(desc[j]); + /* If a register's bit 31 is set, it is an unknown format */ + for (int i =3D 0; i < 4; i++) { + if (regs[i] & (1 << 31)) + regs[i] =3D 0; } + + /* Skip the first byte as it is not a descriptor */ + for (int i =3D 1; i < 16; i++) + intel_tlb_lookup(desc[i]); } =20 static const struct cpu_dev intel_cpu_dev =3D { --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 067D62566E8 for ; Wed, 19 Mar 2025 12:21:52 +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=1742386913; cv=none; b=Ky2sbNXJk7tHown4XbbAEjRPJQf0R6VY5POYP9KfJhDaD+P6W4rwifjozwGDsQAk9iCJH5W4IzGQNgvvSTWRM7ew3ZUyfdt7727g0bzkc3nb3UywPs6hHwzIhYdlNyxnXMWoCSuBSUvi64BRS9rQg65fcd8IHrXcMvhbj0EKEik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386913; c=relaxed/simple; bh=nU4OSc713+t3FWKAhY+Xi2Wo/7zTOFNOruBVPTD2Wi0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iMwautYai92cfUq6J6LwdtDiqnW43wJp0HoHl0ormq5vL1HD527caWw+B3QT3kFYMvjv1yJQjL8mkWHeQex3kEbQ1HQmgpSiEJ/aWxbT7Dk16PHbcf/8iJSKR/HHS+Jk+HVcKf1aweBdqaqkSFQSM4JkXBm7Wmu/mTkzD9L30OE= 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=RAZLbZdQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=P9SHLW4/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RAZLbZdQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="P9SHLW4/" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386910; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xOEE5Ws6vnNWGAOctr95ZFkXsWR7b1uBZjnhDXrVsZY=; b=RAZLbZdQjMvmfctpKxrbBth1AE3xYfxNCwDNdF53LqZjztg+NDwLl8kRQuhk8XOG6VhFQq iQNrf5WumJEQ4tydLXY22+N3fsbKqn7OuK4N3t6VKYszhtWps96bOMbcmuFrbalX8VVGgR D2+Cf6qhX/BmcFbBTPc/8tt1NRACU0rlHwamNYyYqKOkL5D/eNYRFM5VpFotho2Ej8O0VX Fw9S76X1h+4IsSAdpCo3ClzggowZ8EGfFHGChEZkVSMfk+nYTdAfKq9bnGavrMHn35Ms9J HyFP5QlyMf0eek7Mk1KNWVnqgHcipZjNWaSO1eBuzHPDvNrU+DdPyvtICYU5mw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386910; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xOEE5Ws6vnNWGAOctr95ZFkXsWR7b1uBZjnhDXrVsZY=; b=P9SHLW4/QuOtEK8qjl1RmugoB0cpRRkJSfvz/DLVZJhtknvpDxTzJ0v26/3wiPapsf3xVw +OW5iaNu6J4THjDg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 02/29] x86/cacheinfo: Remove leaf 0x2 parsing loop Date: Wed, 19 Mar 2025 13:21:10 +0100 Message-ID: <20250319122137.4004-3-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Leaf 0x2 output includes a "query count" byte where it was supposed to specify the number of repeated CPUID leaf 0x2 subleaf 0 queries needed to extract all of the CPU's cache and TLB descriptors. Per current Intel manuals, all CPUs supporting this leaf "will always" return an iteration count of 1. Remove the leaf 0x2 query loop and just query the hardware once. Note, as previously done at commit aec28d852ed2 ("x86/cpuid: Standardize on u32 in "), standardize on using 'u32' and 'u8' types. Suggested-by: Ingo Molnar Suggested-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 77 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index b3a520959b51..36782fd017b3 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -42,7 +42,7 @@ static cpumask_var_t cpu_cacheinfo_mask; unsigned int memory_caching_control __ro_after_init; =20 struct _cache_table { - unsigned char descriptor; + u8 descriptor; char cache_type; short size; }; @@ -783,50 +783,47 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) =20 /* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level > 1) { - /* supports eax=3D2 call */ - int j, n; - unsigned int regs[4]; - unsigned char *dp =3D (unsigned char *)regs; - - /* Number of times to iterate */ - n =3D cpuid_eax(2) & 0xFF; - - for (i =3D 0 ; i < n ; i++) { - cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); - - /* If bit 31 is set, this is an unknown format */ - for (j =3D 0 ; j < 4 ; j++) - if (regs[j] & (1 << 31)) - regs[j] =3D 0; - - /* Byte 0 is level count, not a descriptor */ - for (j =3D 1 ; j < 16 ; j++) { - unsigned char des =3D dp[j]; - unsigned char k =3D 0; - - /* look up this descriptor in the table */ - while (cache_table[k].descriptor !=3D 0) { - if (cache_table[k].descriptor =3D=3D des) { - switch (cache_table[k].cache_type) { - case LVL_1_INST: - l1i +=3D cache_table[k].size; - break; - case LVL_1_DATA: - l1d +=3D cache_table[k].size; - break; - case LVL_2: - l2 +=3D cache_table[k].size; - break; - case LVL_3: - l3 +=3D cache_table[k].size; - break; - } + u32 regs[4]; + u8 *desc =3D (u8 *)regs; =20 + cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); + + /* Intel CPUs must report an iteration count of 1 */ + if (desc[0] !=3D 0x01) + return; + + /* If a register's bit 31 is set, it is an unknown format */ + for (int i =3D 0; i < 4; i++) { + if (regs[i] & (1 << 31)) + regs[i] =3D 0; + } + + /* Skip the first byte as it is not a descriptor */ + for (int i =3D 1; i < 16; i++) { + u8 des =3D desc[i]; + u8 k =3D 0; + + /* look up this descriptor in the table */ + while (cache_table[k].descriptor !=3D 0) { + if (cache_table[k].descriptor =3D=3D des) { + switch (cache_table[k].cache_type) { + case LVL_1_INST: + l1i +=3D cache_table[k].size; + break; + case LVL_1_DATA: + l1d +=3D cache_table[k].size; + break; + case LVL_2: + l2 +=3D cache_table[k].size; + break; + case LVL_3: + l3 +=3D cache_table[k].size; break; } =20 - k++; + break; } + k++; } } } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 1FEF22561C3 for ; Wed, 19 Mar 2025 12:21:55 +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=1742386916; cv=none; b=EaqWesfv91MY/3LPZZetWWo7qy48VbFdQWVvX3P2DzH7X/XPEFCsnfTTZNbzC7nF/h6H4QNuay1hZ75xS63gbvNBVbEGobyS3hHhzQer1QOI9Qk8IQRhkTQ7jCruF6IVNuen3xz/3dGETLsqGUnltPHLjR6uP/4JMkrXmY0HoDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386916; c=relaxed/simple; bh=HlC2VyZgCA26vEg1uzqeEX+cghnr3JEFvxlGFcxFtS4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d2nB6JhH3sRgGHTB06WWFDZUCHu07IBP7MZvxhR/m3QCB4XHw6Z4KzcbCfx8Z5T3qFNZX9EhuyCEFsrHL7Xfxx4yo0qPCd071bxXhqpBJfC00mm3uYYphEha1W7sm/lbLrkg3EGPpcNROw5iL2w7ivCx3e7fEmyT6DbWI0CT04E= 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=KhR0nYpn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=w0i/RW2i; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KhR0nYpn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="w0i/RW2i" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386913; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/3kKrwykp7K2hYkCCBfT55S/xZ2U7zEFrml/gv7Bp/0=; b=KhR0nYpnjNqMjevlvaavq8Nqn5sJXbVVc5rrZl94ArJYZkX5xnNPn+EQyaBh2Sn5beP7hx 0t9zfwY55v9aKZNRJV1tL/ABq5Wk7gSa9f3ToyDBsyGC2BWiTQIIToWZOwq87xoDIS/540 T26ZizKeN/XkBmBwR9K1koFEG/+akBZTvVmBdmg7y7xyYk9FO9ORJKe7i6QLn8i3DFca/C KscTnFHTopfbSJUjz26TnC2yG0izgW6Ow6ceAmMcVmV10PdXIVQwotLqA7OQ7NS2uvBkZu ERmedpf2Lq3UOEmefUqpQ5MwVDbIj3C0IlTPgQQE5aWuUZ1M3nBa8sw7oNrIoA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386913; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/3kKrwykp7K2hYkCCBfT55S/xZ2U7zEFrml/gv7Bp/0=; b=w0i/RW2ioFlBSM5JAlFSHwasK3uUwwJN1FufSZdJueb5j0PNH3lOdtdLobfVOOW+MRQtlH 3faEMy9ItUUvKAAQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 03/29] x86/cpu: Introduce and use leaf 0x2 parsing helpers Date: Wed, 19 Mar 2025 13:21:11 +0100 Message-ID: <20250319122137.4004-4-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce CPUID leaf 0x2 parsing helpers at . This allows sharing the leaf 0x2's output validation and iteration logic across both x86/cpu intel.c and cacheinfo.c. Start by converting intel.c to the new API. Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid.h | 1 + arch/x86/include/asm/cpuid/leaf_0x2_api.h | 65 +++++++++++++++++++++++ arch/x86/include/asm/cpuid/types.h | 16 ++++++ arch/x86/kernel/cpu/intel.c | 23 +++----- 4 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 arch/x86/include/asm/cpuid/leaf_0x2_api.h diff --git a/arch/x86/include/asm/cpuid.h b/arch/x86/include/asm/cpuid.h index d5749b25fa10..585819331dc6 100644 --- a/arch/x86/include/asm/cpuid.h +++ b/arch/x86/include/asm/cpuid.h @@ -4,5 +4,6 @@ #define _ASM_X86_CPUID_H =20 #include +#include =20 #endif /* _ASM_X86_CPUID_H */ diff --git a/arch/x86/include/asm/cpuid/leaf_0x2_api.h b/arch/x86/include/a= sm/cpuid/leaf_0x2_api.h new file mode 100644 index 000000000000..4c845fc96716 --- /dev/null +++ b/arch/x86/include/asm/cpuid/leaf_0x2_api.h @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_CPUID_LEAF_0x2_API_H +#define _ASM_X86_CPUID_LEAF_0x2_API_H + +#include +#include + +/** + * cpuid_get_leaf_0x2_regs() - Return sanitized leaf 0x2 register output + * @regs: Output parameter + * + * Query CPUID leaf 0x2 and store its output in @regs. Force set any + * invalid 1-byte descriptor returned by the hardware to zero (the NULL + * cache/TLB descriptor) before returning it to the caller. + * + * Use for_each_leaf_0x2_desc() to iterate over the returned output. + */ +static inline void cpuid_get_leaf_0x2_regs(union leaf_0x2_regs *regs) +{ + cpuid_leaf(0x2, regs); + + /* + * All Intel CPUs must report an iteration count of 1. In case + * of bogus hardware, treat all returned descriptors as NULL. + */ + if (regs->desc[0] !=3D 0x01) { + for (int i =3D 0; i < 4; i++) + regs->regv[i] =3D 0; + return; + } + + /* + * The most significant bit (MSB) of each register must be clear. + * If a register is invalid, replace its descriptors with NULL. + */ + for (int i =3D 0; i < 4; i++) { + if (regs->reg[i].invalid) + regs->regv[i] =3D 0; + } +} + +/** + * for_each_leaf_0x2_desc() - Iterator for CPUID leaf 0x2 descriptors + * @regs: Leaf 0x2 output, as returned by cpuid_get_leaf_0x2_regs() + * @desc: Pointer to the returned descriptor for each iteration + * + * Loop over the 1-byte descriptors in the passed leaf 0x2 output registers + * @regs. Provide each descriptor through @desc. + * + * Note that the first byte is skipped as it is not a descriptor. + * + * Sample usage:: + * + * union leaf_0x2_regs regs; + * u8 *desc; + * + * cpuid_get_leaf_0x2_regs(®s); + * for_each_leaf_0x2_desc(regs, desc) { + * // Handle *desc value + * } + */ +#define for_each_leaf_0x2_desc(regs, desc) \ + for (desc =3D &(regs).desc[1]; desc < &(regs).desc[16]; desc++) + +#endif /* _ASM_X86_CPUID_LEAF_0x2_API_H */ diff --git a/arch/x86/include/asm/cpuid/types.h b/arch/x86/include/asm/cpui= d/types.h index 8582e27e836d..753f6c4514f4 100644 --- a/arch/x86/include/asm/cpuid/types.h +++ b/arch/x86/include/asm/cpuid/types.h @@ -29,4 +29,20 @@ enum cpuid_regs_idx { #define CPUID_LEAF_FREQ 0x16 #define CPUID_LEAF_TILE 0x1d =20 +/* + * Types for CPUID(0x2) parsing + * Check + */ + +struct leaf_0x2_reg { + u32 : 31, + invalid : 1; +}; + +union leaf_0x2_regs { + struct leaf_0x2_reg reg[4]; + u32 regv[4]; + u8 desc[16]; +}; + #endif /* _ASM_X86_CPUID_TYPES_H */ diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 0570d4d86006..aeb7d6d48379 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -778,27 +779,15 @@ static void intel_tlb_lookup(const unsigned char desc) =20 static void intel_detect_tlb(struct cpuinfo_x86 *c) { - u32 regs[4]; - u8 *desc =3D (u8 *)regs; + union leaf_0x2_regs regs; + u8 *desc; =20 if (c->cpuid_level < 2) return; =20 - cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); - - /* Intel CPUs must report an iteration count of 1 */ - if (desc[0] !=3D 0x01) - return; - - /* If a register's bit 31 is set, it is an unknown format */ - for (int i =3D 0; i < 4; i++) { - if (regs[i] & (1 << 31)) - regs[i] =3D 0; - } - - /* Skip the first byte as it is not a descriptor */ - for (int i =3D 1; i < 16; i++) - intel_tlb_lookup(desc[i]); + cpuid_get_leaf_0x2_regs(®s); + for_each_leaf_0x2_desc(regs, desc) + intel_tlb_lookup(*desc); } =20 static const struct cpu_dev intel_cpu_dev =3D { --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 A7F1F2571CE for ; Wed, 19 Mar 2025 12:21:58 +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=1742386920; cv=none; b=WXMgQ1HBabBWqCJCTp5w+tVfYzCcGgRPrVW6fX6Goejk1UTG72Nq13YKxLZFve4ZHf6max+z6E8sdlMJdnfMqOp2RzCrabenHi6sl5njq+NaM2qcuO4P8FiUecGs//3/HFDmqeNMzIKe+yjpn/iSTlncuaNYlo6iE2zbb4XNW14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386920; c=relaxed/simple; bh=W3GXBnfLsCIM3g0I11/5obqb72B18v4dzsCQ6S4pDqs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JaV4FdQK9xpEAVAmqGh/Yq42HzU0oSHb8Jwj14EEB0vHhcYcIoU9tfklKNsG66ruFq31kxA8TN2XDvF52zzGkyQV50UenNcUUX+DdQ3s+Ds5Lq8ATqRAsLriYVeoZ5SsH1Fp0GarzaUftybXYYHPh7bjp6uNhRUmthDng70jdfo= 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=oCGQbpMc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uZEvtxd/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oCGQbpMc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uZEvtxd/" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386916; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZxA17mUjvVkz0uVhl5/KFi+0Clr/rkFfhSUGfgn+GZU=; b=oCGQbpMcA+8a27CBusw/StvJBHexY+IIloXJeZBGo+sZ9075uq+o1+EkYuZ8GDR87A16LM vL3KqpKg1BWXzli25IZmbzMnU/7G3LetnkqO2QHXtLwZyArxO1sOTkPo9EhQe4kCXuEq9K BLWpYXA8fcHFDvSZ5ZEbXODjo76nAVIy2yeyhrV0qPAPvA1Ew6X600UmAKBOyb0/ytZzez jFtvJ8PBGuPF2wFbPe98XWeu2IUf4U1BBLFMY/WOV/gQ7Mg1VA+nVmOFzi5g3j7XHDOGVH IF7WF5M2yugELUZeMC2Fwta++gj5l+1/hgekDf9/TqLhYj6vEppgxzVwASPyKw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386916; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZxA17mUjvVkz0uVhl5/KFi+0Clr/rkFfhSUGfgn+GZU=; b=uZEvtxd/Exqjh4iV0+AfkEJmiZbWzjvjfnvoBieM/O/Z7pVTTsHKuouLijoPzZxbvccFc1 FXcTcKU28VpAF8AQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 04/29] x86/cacheinfo: Use leaf 0x2 parsing helpers Date: Wed, 19 Mar 2025 13:21:12 +0100 Message-ID: <20250319122137.4004-5-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Parent commit introduced CPUID leaf 0x2 parsing helpers at . The new API allows sharing leaf 0x2's output validation and iteration logic across both intel.c and cacheinfo.c. Convert cacheinfo.c to that new API. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 36782fd017b3..6c610805e356 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -783,29 +784,16 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) =20 /* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level > 1) { - u32 regs[4]; - u8 *desc =3D (u8 *)regs; + union leaf_0x2_regs regs; + u8 *desc; =20 - cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]); - - /* Intel CPUs must report an iteration count of 1 */ - if (desc[0] !=3D 0x01) - return; - - /* If a register's bit 31 is set, it is an unknown format */ - for (int i =3D 0; i < 4; i++) { - if (regs[i] & (1 << 31)) - regs[i] =3D 0; - } - - /* Skip the first byte as it is not a descriptor */ - for (int i =3D 1; i < 16; i++) { - u8 des =3D desc[i]; + cpuid_get_leaf_0x2_regs(®s); + for_each_leaf_0x2_desc(regs, desc) { u8 k =3D 0; =20 /* look up this descriptor in the table */ while (cache_table[k].descriptor !=3D 0) { - if (cache_table[k].descriptor =3D=3D des) { + if (cache_table[k].descriptor =3D=3D *desc) { switch (cache_table[k].cache_type) { case LVL_1_INST: l1i +=3D cache_table[k].size; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 DEE4A257421 for ; Wed, 19 Mar 2025 12:22:01 +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=1742386923; cv=none; b=EERfQNYRdicMiTUs0DjGmbe7S9U1eP+3Juq9thYJhhrvQvMv7zQdk/h2LDhUQMilTfLLHnt7nP8WvN1Ph3l7klm6C6UlPZyUfL8NoxANcRmd024dksBdksL0TOHH1qV4iGijGdmouqzzlTfOH+yiZF+qTJP3At8adyBo5yL9+Wc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386923; c=relaxed/simple; bh=M+UjdWU1Oj03lS1BlbZCoMM+wkEo5EnfsyMkuYsbMLY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cBNETy4/VoWszfeSyi8COghKKiUWV8UEevk2SHVhe6QBVd7UNDCmrsudDJ0s5E5p2WLu9xOyPwXWx6QiLydy4wOGM21x/75gR4HBi4lVk70GQRLCOXTj9LPRC5zL9xVjaH3aTes8iNDqom58QmrVVumhrhYUodeKCBk37gcnQAw= 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=1IwEPkpZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=p2+nR7PM; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1IwEPkpZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="p2+nR7PM" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386920; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d1xoRCk3B1mA4GEq9/A/G6CTAf5k+cJfXB1LIKxQQJ4=; b=1IwEPkpZgnMwtsp1PYlt1UqjMlBm/dsGTJQwR548y17beRDOkkl7RC2kCva/Z0oLGofmVe /wIxBLcVoQpXKmC91opPFmuHI5ssGBwhS5YmE0+qlH7ZOwrSFRXhx+Wk+SohqH/MD9VtcP uuMVNHtgVb/IHygW/q1ePFVsh6XOxrkl9NngZWfDD4x0vO/fV/U0oSGR8v1lHHCosZBbRJ Eo+RzqpYWR2Dy4+aeIqPHWm/jE/BHSTj8TMdwu4oFHdrXQAsVppBShsQGK8vZ06WgOPWMm xULo5bJ6zKogDm0LUbaPw3zGw2VWoXltS4gBwqWlWVCEsIr+Ywk99apxK8ZXtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386920; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d1xoRCk3B1mA4GEq9/A/G6CTAf5k+cJfXB1LIKxQQJ4=; b=p2+nR7PM41UhljpAoDvOKuYY92TK8cEeUj4SXEtqbVm84hAbvesmgURhg9LBJ1Ibpb4jBI H9+/BzYS2beetUDA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 05/29] x86/cacheinfo: Refactor leaf 0x2 cache descriptor lookup Date: Wed, 19 Mar 2025 13:21:13 +0100 Message-ID: <20250319122137.4004-6-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Extract the cache descriptor lookup logic out of the leaf 0x2 parsing code and into a dedicated function. This disentangles such lookup from the deeply nested leaf 0x2 parsing loop. Remove the cache table termination entry, as it is no longer needed after the ARRAY_SIZE()-based lookup. [ darwi: Move refactoring logic into this separate commit + commit log. Remove the cache table termination entry. ] Signed-off-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 44 +++++++++++++++------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 6c610805e356..d0bfdb85b96a 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -123,7 +123,6 @@ static const struct _cache_table cache_table[] =3D { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */ { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */ { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */ - { 0x00, 0, 0} }; =20 =20 @@ -728,6 +727,16 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =3D find_num_cache_leaves(c); } =20 +static const struct _cache_table *cache_table_get(u8 desc) +{ + for (int i =3D 0; i < ARRAY_SIZE(cache_table); i++) { + if (cache_table[i].descriptor =3D=3D desc) + return &cache_table[i]; + } + + return NULL; +} + void init_intel_cacheinfo(struct cpuinfo_x86 *c) { /* Cache sizes */ @@ -784,34 +793,21 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) =20 /* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level > 1) { + const struct _cache_table *entry; union leaf_0x2_regs regs; u8 *desc; =20 cpuid_get_leaf_0x2_regs(®s); for_each_leaf_0x2_desc(regs, desc) { - u8 k =3D 0; - - /* look up this descriptor in the table */ - while (cache_table[k].descriptor !=3D 0) { - if (cache_table[k].descriptor =3D=3D *desc) { - switch (cache_table[k].cache_type) { - case LVL_1_INST: - l1i +=3D cache_table[k].size; - break; - case LVL_1_DATA: - l1d +=3D cache_table[k].size; - break; - case LVL_2: - l2 +=3D cache_table[k].size; - break; - case LVL_3: - l3 +=3D cache_table[k].size; - break; - } - - break; - } - k++; + entry =3D cache_table_get(*desc); + if (!entry) + continue; + + switch (entry->cache_type) { + case LVL_1_INST: l1i +=3D entry->size; break; + case LVL_1_DATA: l1d +=3D entry->size; break; + case LVL_2: l2 +=3D entry->size; break; + case LVL_3: l3 +=3D entry->size; break; } } } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 6AF1925744B for ; Wed, 19 Mar 2025 12:22: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=1742386927; cv=none; b=p0ugyMPzuV8Loc+nUun0D0JEZ94prtlkds7WWjQ7a10O0uwOW3/F04WHiHVTmlus3N+s8FQS6XZKJK/cFPDHfdDXcfu//Hn2vSbbiWv+Iod/D8MGc94zgMKT8Zkw7S+rk0V+GCX3PFA75iEj22asJ+p/bJCk8eegMyhu7Iwx+70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386927; c=relaxed/simple; bh=+J2MUMA6FaqN89uo/gT4W5HA2WW9NsIVSlCj58sAbXw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O1t7HPExPjEeUUyLN8MLWmJNhl83G4H/RLLZPz5GfMBQT80sgIe34mTz9oI5b87138p++RtgyqovnPAWL4By4+s8av+W/tMhSotROK+39JVCBrL/uHTfzQjFhrWhMg5VJvkqVZDu/wT+JU7nuzl8YNCdMKHU7Ix94v/JkhT74eM= 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=wJRZb9Sw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=8mfIAG+/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wJRZb9Sw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="8mfIAG+/" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386923; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cjwIQbDd6xf3kEosT4buGrof2c51/KArCUqdt8u05RA=; b=wJRZb9Swaq9pxAQBIogcZ5u1qrhsck121ovo8iQVzWf2l0PAfJkszyfvYq67zhoupC65gY 5Omx/zFYyQSC+r/cZPLAFuwN7YY1znFEzrpc5wlfIwavbx7W8CPBplnopNW+qoDw0V9vCo IcnWD2BBb6sHCiZkhXF6SLwSdz77Y7NfLBNxolVajNBF7sHhSTPsU2wlFZT2uN0boxjltR BWx0d47IAR0z48viidbleCqxYmg2p9CIdq9qpozIfolB6Yxp7sGtXlDmmVleLnq7NRkXlX gwwDPWovBA+gA++llSepnu5hwHgY42KDz6CrTXqrqoPtxe0cMyHYb00ZkjEx1A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386923; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cjwIQbDd6xf3kEosT4buGrof2c51/KArCUqdt8u05RA=; b=8mfIAG+/1YH2hTSO90a4DDuVTDmjYbrqsFwW0KJtPCdv4XH8mqOb9VJilLd81+56sqa9Nd pgROUY3QIumOMzBA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 06/29] x86/cacheinfo: Properly name amd_cpuid4()'s first parameter Date: Wed, 19 Mar 2025 13:21:14 +0100 Message-ID: <20250319122137.4004-7-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner amd_cpuid4()'s first parameter, "leaf", is not a CPUID leaf as the name implies. Rather, it's an index emulating CPUID(4)'s subleaf semantics; i.e. an ID for the cache object currently enumerated. Rename that parameter to "index". Apply minor coding style fixes to the rest of the function as well. [ darwi: Move into a separate commit and write commit log. Use "index" instead of "subleaf" for amd_cpuid4() first param, as that's the name typically used at the whole of cacheinfo.c. ] Signed-off-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index d0bfdb85b96a..0fd4e9673665 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -233,12 +233,10 @@ static const enum cache_type cache_type_map[] =3D { }; =20 static void -amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax, - union _cpuid4_leaf_ebx *ebx, - union _cpuid4_leaf_ecx *ecx) +amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, + union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx) { - unsigned dummy; - unsigned line_size, lines_per_tag, assoc, size_in_kb; + unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb; union l1_cache l1i, l1d; union l2_cache l2; union l3_cache l3; @@ -251,7 +249,7 @@ amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax, cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val); cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val); =20 - switch (leaf) { + switch (index) { case 1: l1 =3D &l1i; fallthrough; @@ -289,12 +287,11 @@ amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax, } =20 eax->split.is_self_initializing =3D 1; - eax->split.type =3D types[leaf]; - eax->split.level =3D levels[leaf]; + eax->split.type =3D types[index]; + eax->split.level =3D levels[index]; eax->split.num_threads_sharing =3D 0; eax->split.num_cores_on_die =3D topology_num_cores_per_package(); =20 - if (assoc =3D=3D 0xffff) eax->split.is_fully_associative =3D 1; ebx->split.coherency_line_size =3D line_size - 1; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 7C1CD257AD1 for ; Wed, 19 Mar 2025 12:22:07 +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=1742386929; cv=none; b=rB3ZBlWjxBPLTk84wtmA47AWbgsgsFLKMLC7D0K+MapXM8ApPbJgSZkwyDO1g/1iJyFVw61urlmBEdfJA0JikpgeTnr0fOowQI15352yLyEm8smYQih08KpH0M07z7sNu6rMu9MPHmF6Cp+dVeR2Fbfu5b431R58ASjsFkso1dU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386929; c=relaxed/simple; bh=znWoNYld3787/FLJoNVvFUE6W3u5t2TBlMBak9EfeNM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gTcvf7smSGb5rS9udzcapjNb3ouQtr2Yj/qnIqKHT8Vjk0E43l6BkB9DmllZ+BiOTha9gqRVN7crS3YIkrvWYBFVJ4md0dqi9z+j7YNEtGaguKxodEVi6HvWT7DfUuFV+Nwz6E5qoXCfagA9cfzR5Hv0GLUIFeyufj7OPkZbKJw= 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=qHPzhg+i; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0QbOAGji; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qHPzhg+i"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0QbOAGji" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H07r9TClFMn3r/eznmGz5IHals7y/XHqVXwP99shnE8=; b=qHPzhg+iX5PHLROHMUW1du6koUjVTR60vOZotjHeCvETsuPSeH7SXYYvNC0pNnCBcBz10c 2MRqVCI8ZYcbUjREktnctOgAWKGisMDOiNqZ7SQGcFhNA25HEfN8nIRAw0ucEPt8dNqBQG eIAGDBvvEtchTn2bxUanmFdJSAN69HI1XuofmD6gaV+wmc/I9NlssGUrHymuvhPtN8JYLL VUmVwC+eyIYYWNiWL094Moxx/WUh31U0sY4hSY7xgNwHgRGoCjkWVFy3/Zn7pcg7kVbrYY jzWsZsY/F2RochR7LsW6QrfSNlpsgBzjv3gk3xYTCHFa77nprEUX9IFuU5mebg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H07r9TClFMn3r/eznmGz5IHals7y/XHqVXwP99shnE8=; b=0QbOAGjipMc7dXVg9kgKviwVcAbCTzNcIZ6lcwGGlTyeG0zYnHW892BikMFANcMnTVx9qd 0d+6sHsr4fGa5kDg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 07/29] x86/cacheinfo: Use proper name for cacheinfo instances Date: Wed, 19 Mar 2025 13:21:15 +0100 Message-ID: <20250319122137.4004-8-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner The cacheinfo structure defined at is a generic cache info object representation. Calling its instances at x86 cacheinfo.c "leaf" confuses it with a CPUID leaf -- especially that multiple CPUID calls are already sprinkled across that file. Most of such instances also have a redundant "this_" prefix. Rename all of the cacheinfo "this_leaf" instances to just "ci". [ darwi: Move into separate commit and write commit log ] Signed-off-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 89 ++++++++++++++++----------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 0fd4e9673665..be9be5e56b44 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -349,11 +349,10 @@ static int amd_get_l3_disable_slot(struct amd_northbr= idge *nb, unsigned slot) return -1; } =20 -static ssize_t show_cache_disable(struct cacheinfo *this_leaf, char *buf, - unsigned int slot) +static ssize_t show_cache_disable(struct cacheinfo *ci, char *buf, unsigne= d int slot) { int index; - struct amd_northbridge *nb =3D this_leaf->priv; + struct amd_northbridge *nb =3D ci->priv; =20 index =3D amd_get_l3_disable_slot(nb, slot); if (index >=3D 0) @@ -367,8 +366,8 @@ static ssize_t \ cache_disable_##slot##_show(struct device *dev, \ struct device_attribute *attr, char *buf) \ { \ - struct cacheinfo *this_leaf =3D dev_get_drvdata(dev); \ - return show_cache_disable(this_leaf, buf, slot); \ + struct cacheinfo *ci =3D dev_get_drvdata(dev); \ + return show_cache_disable(ci, buf, slot); \ } SHOW_CACHE_DISABLE(0) SHOW_CACHE_DISABLE(1) @@ -435,18 +434,17 @@ static int amd_set_l3_disable_slot(struct amd_northbr= idge *nb, int cpu, return 0; } =20 -static ssize_t store_cache_disable(struct cacheinfo *this_leaf, - const char *buf, size_t count, - unsigned int slot) +static ssize_t store_cache_disable(struct cacheinfo *ci, const char *buf, + size_t count, unsigned int slot) { unsigned long val =3D 0; int cpu, err =3D 0; - struct amd_northbridge *nb =3D this_leaf->priv; + struct amd_northbridge *nb =3D ci->priv; =20 if (!capable(CAP_SYS_ADMIN)) return -EPERM; =20 - cpu =3D cpumask_first(&this_leaf->shared_cpu_map); + cpu =3D cpumask_first(&ci->shared_cpu_map); =20 if (kstrtoul(buf, 10, &val) < 0) return -EINVAL; @@ -467,8 +465,8 @@ cache_disable_##slot##_store(struct device *dev, \ struct device_attribute *attr, \ const char *buf, size_t count) \ { \ - struct cacheinfo *this_leaf =3D dev_get_drvdata(dev); \ - return store_cache_disable(this_leaf, buf, count, slot); \ + struct cacheinfo *ci =3D dev_get_drvdata(dev); \ + return store_cache_disable(ci, buf, count, slot); \ } STORE_CACHE_DISABLE(0) STORE_CACHE_DISABLE(1) @@ -476,8 +474,8 @@ STORE_CACHE_DISABLE(1) static ssize_t subcaches_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct cacheinfo *this_leaf =3D dev_get_drvdata(dev); - int cpu =3D cpumask_first(&this_leaf->shared_cpu_map); + struct cacheinfo *ci =3D dev_get_drvdata(dev); + int cpu =3D cpumask_first(&ci->shared_cpu_map); =20 return sprintf(buf, "%x\n", amd_get_subcaches(cpu)); } @@ -486,8 +484,8 @@ static ssize_t subcaches_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct cacheinfo *this_leaf =3D dev_get_drvdata(dev); - int cpu =3D cpumask_first(&this_leaf->shared_cpu_map); + struct cacheinfo *ci =3D dev_get_drvdata(dev); + int cpu =3D cpumask_first(&ci->shared_cpu_map); unsigned long val; =20 if (!capable(CAP_SYS_ADMIN)) @@ -511,10 +509,10 @@ cache_private_attrs_is_visible(struct kobject *kobj, struct attribute *attr, int unused) { struct device *dev =3D kobj_to_dev(kobj); - struct cacheinfo *this_leaf =3D dev_get_drvdata(dev); + struct cacheinfo *ci =3D dev_get_drvdata(dev); umode_t mode =3D attr->mode; =20 - if (!this_leaf->priv) + if (!ci->priv) return 0; =20 if ((attr =3D=3D &dev_attr_subcaches.attr) && @@ -562,11 +560,11 @@ static void init_amd_l3_attrs(void) } =20 const struct attribute_group * -cache_get_priv_group(struct cacheinfo *this_leaf) +cache_get_priv_group(struct cacheinfo *ci) { - struct amd_northbridge *nb =3D this_leaf->priv; + struct amd_northbridge *nb =3D ci->priv; =20 - if (this_leaf->level < 3 || !nb) + if (ci->level < 3 || !nb) return NULL; =20 if (nb && nb->l3_cache.indices) @@ -846,7 +844,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, struct _cpuid4_info_regs *base) { struct cpu_cacheinfo *this_cpu_ci; - struct cacheinfo *this_leaf; + struct cacheinfo *ci; int i, sibling; =20 /* @@ -858,12 +856,12 @@ static int __cache_amd_cpumap_setup(unsigned int cpu,= int index, this_cpu_ci =3D get_cpu_cacheinfo(i); if (!this_cpu_ci->info_list) continue; - this_leaf =3D this_cpu_ci->info_list + index; + ci =3D this_cpu_ci->info_list + index; for_each_cpu(sibling, cpu_llc_shared_mask(cpu)) { if (!cpu_online(sibling)) continue; cpumask_set_cpu(sibling, - &this_leaf->shared_cpu_map); + &ci->shared_cpu_map); } } } else if (boot_cpu_has(X86_FEATURE_TOPOEXT)) { @@ -883,14 +881,14 @@ static int __cache_amd_cpumap_setup(unsigned int cpu,= int index, if ((apicid < first) || (apicid > last)) continue; =20 - this_leaf =3D this_cpu_ci->info_list + index; + ci =3D this_cpu_ci->info_list + index; =20 for_each_online_cpu(sibling) { apicid =3D cpu_data(sibling).topo.apicid; if ((apicid < first) || (apicid > last)) continue; cpumask_set_cpu(sibling, - &this_leaf->shared_cpu_map); + &ci->shared_cpu_map); } } } else @@ -903,7 +901,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int = index, struct _cpuid4_info_regs *base) { struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); - struct cacheinfo *this_leaf, *sibling_leaf; + struct cacheinfo *ci, *sibling_ci; unsigned long num_threads_sharing; int index_msb, i; struct cpuinfo_x86 *c =3D &cpu_data(cpu); @@ -914,10 +912,10 @@ static void __cache_cpumap_setup(unsigned int cpu, in= t index, return; } =20 - this_leaf =3D this_cpu_ci->info_list + index; + ci =3D this_cpu_ci->info_list + index; num_threads_sharing =3D 1 + base->eax.split.num_threads_sharing; =20 - cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map); + cpumask_set_cpu(cpu, &ci->shared_cpu_map); if (num_threads_sharing =3D=3D 1) return; =20 @@ -929,28 +927,27 @@ static void __cache_cpumap_setup(unsigned int cpu, in= t index, =20 if (i =3D=3D cpu || !sib_cpu_ci->info_list) continue;/* skip if itself or no cacheinfo */ - sibling_leaf =3D sib_cpu_ci->info_list + index; - cpumask_set_cpu(i, &this_leaf->shared_cpu_map); - cpumask_set_cpu(cpu, &sibling_leaf->shared_cpu_map); + sibling_ci =3D sib_cpu_ci->info_list + index; + cpumask_set_cpu(i, &ci->shared_cpu_map); + cpumask_set_cpu(cpu, &sibling_ci->shared_cpu_map); } } =20 -static void ci_leaf_init(struct cacheinfo *this_leaf, - struct _cpuid4_info_regs *base) +static void ci_info_init(struct cacheinfo *ci, struct _cpuid4_info_regs *b= ase) { - this_leaf->id =3D base->id; - this_leaf->attributes =3D CACHE_ID; - this_leaf->level =3D base->eax.split.level; - this_leaf->type =3D cache_type_map[base->eax.split.type]; - this_leaf->coherency_line_size =3D + ci->id =3D base->id; + ci->attributes =3D CACHE_ID; + ci->level =3D base->eax.split.level; + ci->type =3D cache_type_map[base->eax.split.type]; + ci->coherency_line_size =3D base->ebx.split.coherency_line_size + 1; - this_leaf->ways_of_associativity =3D + ci->ways_of_associativity =3D base->ebx.split.ways_of_associativity + 1; - this_leaf->size =3D base->size; - this_leaf->number_of_sets =3D base->ecx.split.number_of_sets + 1; - this_leaf->physical_line_partition =3D + ci->size =3D base->size; + ci->number_of_sets =3D base->ecx.split.number_of_sets + 1; + ci->physical_line_partition =3D base->ebx.split.physical_line_partition + 1; - this_leaf->priv =3D base->nb; + ci->priv =3D base->nb; } =20 int init_cache_level(unsigned int cpu) @@ -984,7 +981,7 @@ int populate_cache_leaves(unsigned int cpu) { unsigned int idx, ret; struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); - struct cacheinfo *this_leaf =3D this_cpu_ci->info_list; + struct cacheinfo *ci =3D this_cpu_ci->info_list; struct _cpuid4_info_regs id4_regs =3D {}; =20 for (idx =3D 0; idx < this_cpu_ci->num_leaves; idx++) { @@ -992,7 +989,7 @@ int populate_cache_leaves(unsigned int cpu) if (ret) return ret; get_cache_id(cpu, &id4_regs); - ci_leaf_init(this_leaf++, &id4_regs); + ci_info_init(ci++, &id4_regs); __cache_cpumap_setup(cpu, idx, &id4_regs); } this_cpu_ci->cpu_map_populated =3D true; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 97486257AF9 for ; Wed, 19 Mar 2025 12:22:10 +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=1742386932; cv=none; b=lcdYCduPWzNuJchhCDUsTAPAQaGz4FNB8uXu/zHJNK3p1GtIOz7pwo/XqZbH90G2ke6Y6kx8zf1GUGc3XxdUkoTLv1qgEH5QULqfGqh/AVm+oEy0oLafYmGm1aVHJoPzDTVezi4w2Rkkqg3Q2qxRQXuJZpuv/Q/E+4+WLbQYvhw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386932; c=relaxed/simple; bh=GZolcfzm1JnZkKqjVbXM2gj9PLpHcVbDueFdV984WbI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KuYzjvIQVO1cO1kJo7OpyIlwpBHSY7MtAmb47jJHqRkYuC4P3lKRvUaNq6wOZgDSs5/7TCZaWTYlr87rfVsD9/hRqxeJTLYSBHEFAQ1kfDzoehZQh/7M8gO34OdJtu8DbFiV8AZ6LbSW8plHPFUCF9tpNzyZ+utTVVzA5ONmh3M= 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=N1gezVie; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=SFpjDIRb; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="N1gezVie"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="SFpjDIRb" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386929; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7IZo4crAyQCBNwnRyQzdxn5oekpXAzTH9DnGF+58/1U=; b=N1gezVieGnwnwclnj1gURR2OHiJZPEMUS9V/hcEtnF/7X/bnHMsTluebKp3IdgjItEPW8G ahO6UkLHFsPsoZJBBRPRQui/gY2BWUe43mXZbSKp+pXtCKebRz9pBfxZbO+b8oitAnPwDD 8i+a9r6qyeLBHpQuWpf8PnSM5USMzoT4rmt5qzMjFCsJz1fsiFsmRSDqgt0VIoP+dvKsqm PhToVnrXlAQQ25kgJZnu/fxLNAO4UJ49YzNYkBzzq7kTSLahFSnQZcO7qU2HLXVgYOxiPp XqE1MIqA72f+dB6PPQjmEhTQ7Tyqt1Vvgem7BPGs94GanE1COCE2f4Z/yZDuFg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386929; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7IZo4crAyQCBNwnRyQzdxn5oekpXAzTH9DnGF+58/1U=; b=SFpjDIRbYITPtuDTmc5Xh2+4YTXeSM93cn97JbKWw7eZcQRvRCCjg4WT3JSu+qSJUzSEvQ 5McFjaezAi5rBVDw== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 08/29] x86/cacheinfo: Constify _cpuid4_info_regs instances Date: Wed, 19 Mar 2025 13:21:16 +0100 Message-ID: <20250319122137.4004-9-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" _cpuid4_info_regs instances are passed through a large number of functions at cacheinfo.c. For clarity, constify the instance parameters where _cpuid4_info_regs is only read from. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index be9be5e56b44..fc4b49ec42df 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -841,7 +841,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) } =20 static int __cache_amd_cpumap_setup(unsigned int cpu, int index, - struct _cpuid4_info_regs *base) + const struct _cpuid4_info_regs *base) { struct cpu_cacheinfo *this_cpu_ci; struct cacheinfo *ci; @@ -898,7 +898,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, } =20 static void __cache_cpumap_setup(unsigned int cpu, int index, - struct _cpuid4_info_regs *base) + const struct _cpuid4_info_regs *base) { struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci, *sibling_ci; @@ -933,7 +933,8 @@ static void __cache_cpumap_setup(unsigned int cpu, int = index, } } =20 -static void ci_info_init(struct cacheinfo *ci, struct _cpuid4_info_regs *b= ase) +static void ci_info_init(struct cacheinfo *ci, + const struct _cpuid4_info_regs *base) { ci->id =3D base->id; ci->attributes =3D CACHE_ID; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 709C32580FE for ; Wed, 19 Mar 2025 12:22:14 +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=1742386936; cv=none; b=X9i7VlV+sh4U1fEa/dWCMbE/rDK3Fj98+zS9Ki1YfvWp+b7v+lZxfvx/SJyhuj5wdeUGfD3kF3ppmJBs2dBu7PFmaUaBDeEpceT5rlNclZ83/07wwTTi3QFYtFTPH92Vvf1LOhaYpugjohO301hK7OHfWQ5ds6kKwNpyzDrTVAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386936; c=relaxed/simple; bh=D6MoNQnMIu0yWa3uh0hhdaUOlxNQ3SZzfhKRe7XAQRk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uDOdgKv9MR473B7MzZKPzLpvXNiRB5AauuR9L82EHuh1FYw8yx6qVWs994y9SdEgHUP2ols2fi+qaXBf9mnHhBh7ZPrF2mmCE5lidgvz3EFYdrznlIn+It2GLyKMfifJPh80e0UYe59LbAavTipJtuY/M0kToCeg7NvB4LO8kkQ= 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=cKN6TFGq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=692R01hy; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cKN6TFGq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="692R01hy" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386932; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3Yz7MYWtdIz2Z6Ew7EmnlMJcHLa1Fmv3VryPW4eNU0Y=; b=cKN6TFGqQkh1IplLUlvlE+h6I5SNYrQQg2bk2Bz/ZcdgpSXcqsSJIj/yc1BKrUHRIppGdS U7+c1NipWyVtOSeQEIkWyDK1Po4aAteWSu9T8jjLOu6/b4qrQSwghbgU4TiQNlP8pVdJFO HAtkRzlxgHav3vexZ5xQbKgyvriPrrHlv7ekdVMrQg7sQes+G3JGfVtXeRLLQyjvp5oJcO cmGqzOibR5B1SAndkGMOm4R0mJP4NKaWLTSbg0rP0ZvSzb7J3mHlgY1zQQ7HQNIC3jtY0g DimPS6mFky5RnbKb+Q101kDZjlkVbDFk2TFlGSYySMM2zZfuYlCBFdDGwXIJVg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386932; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3Yz7MYWtdIz2Z6Ew7EmnlMJcHLa1Fmv3VryPW4eNU0Y=; b=692R01hywIZC12p+XVEjX+oXmZfQp1paMKkGtsrW3MLFqHZ6xIdkZ6erkhPGJMXeRRSXGD J4fkG6af0p1qeABg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 09/29] x86/cacheinfo: Align ci_info_init() assignment expressions Date: Wed, 19 Mar 2025 13:21:17 +0100 Message-ID: <20250319122137.4004-10-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ci_info_init() function initializes 10 members of a struct cacheinfo instance using passed data from CPUID leaf 0x4. Such assignment expressions are difficult to read in their current form. Align them for clarity. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index fc4b49ec42df..b273ecf3f538 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -936,19 +936,16 @@ static void __cache_cpumap_setup(unsigned int cpu, in= t index, static void ci_info_init(struct cacheinfo *ci, const struct _cpuid4_info_regs *base) { - ci->id =3D base->id; - ci->attributes =3D CACHE_ID; - ci->level =3D base->eax.split.level; - ci->type =3D cache_type_map[base->eax.split.type]; - ci->coherency_line_size =3D - base->ebx.split.coherency_line_size + 1; - ci->ways_of_associativity =3D - base->ebx.split.ways_of_associativity + 1; - ci->size =3D base->size; - ci->number_of_sets =3D base->ecx.split.number_of_sets + 1; - ci->physical_line_partition =3D - base->ebx.split.physical_line_partition + 1; - ci->priv =3D base->nb; + ci->id =3D base->id; + ci->attributes =3D CACHE_ID; + ci->level =3D base->eax.split.level; + ci->type =3D cache_type_map[base->eax.split.type]; + ci->coherency_line_size =3D base->ebx.split.coherency_line_size + 1; + ci->ways_of_associativity =3D base->ebx.split.ways_of_associativity + 1; + ci->size =3D base->size; + ci->number_of_sets =3D base->ecx.split.number_of_sets + 1; + ci->physical_line_partition =3D base->ebx.split.physical_line_partition += 1; + ci->priv =3D base->nb; } =20 int init_cache_level(unsigned int cpu) --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 EE9EE2586C3 for ; Wed, 19 Mar 2025 12:22:16 +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=1742386938; cv=none; b=rnXCh4be9s7QWiSyEwrr2Vyd5jW8EXnWKxihmx+Ks3Xsf0Mq5e8y+QACwc95T+MDm7JCkEA/VUeyNZ9tCgN+dvaFBua5X2nrCxHptKwaXZRXzZ4+dCZKG8PKZW69/2NWYZX90YKAWo6z8ua0Ihx6VR9SutSWD/mqOy4r/r52Yog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386938; c=relaxed/simple; bh=aE016W8b9s8G1p7pMupUW4RrDDEGtwwRil1/PDlHSqI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HbnDSDfFnpcF7CF4BwbRsX5e8mItBKu90DkeU8F/fNpB54Y/vsTnP6X1pjeMANZSshgHUj7K0+8CJynujeiBIaX+Tll7DqxazK2ajKrTPgcjkv42KzQsqsFW1i1OksNLBJXD2xdgbyBAIZp5GKsZjY+DG4lO791q7nxu4gNYLf4= 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=sDGOPGYA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qwFtRq91; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sDGOPGYA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qwFtRq91" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386935; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NIS91U2B+xF0EmmM6VIYVMN1YLL1xaHjZJ8dOVwZViY=; b=sDGOPGYAbmejRvFa1kEIwz7Z277/kdrW3SIQXjoOwk+hqhPvY63dlNGkgG/J+6/mzjddv3 s3rb7bQO6Lf3jdYmF0w3Neu5AU+V59mGGxQjNhiSwtAX9URnL3UUAFtrltdiJfkOAqBuP0 Qi8jG8DUuYEyq4yPPRlkS9zayuyaJYI29ifhAJVw0SnOGM+kfBT3ucnOX7ZQ7WRACkd1tg TRLxDyKdId6RIR12ecF0rSFn55Sy33riTQHAQ1gBwMYeyYGvY6RgZpeJr7K4MtjmM1LYyI oqAnhp5hVxL+zSalhRbKu5zd2zB/fvs0KrGMhPmpZWiRG7tlt6UrFu9ySwouxw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386935; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NIS91U2B+xF0EmmM6VIYVMN1YLL1xaHjZJ8dOVwZViY=; b=qwFtRq91QngcJ13824oXWPcxRUax+DcFqYWQ68tITOHmbrZm4zsouOX5sH+jHyezhqzka7 fHdAG+LXGA4pV9Dw== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 10/29] x86/cacheinfo: Standardize _cpuid4_info_regs instance naming Date: Wed, 19 Mar 2025 13:21:18 +0100 Message-ID: <20250319122137.4004-11-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The cacheinfo code frequently uses the output registers from CPUID leaf 0x4. Such registers are cached at struct _cpuid4_info_regs, augmented with related information, and are then passed across functions. The naming of these _cpuid4_info_regs instances is confusing at best. Some instances are called "this_leaf", which is vague as "this" lacks context and "leaf" is overly generic given that other CPUID leaves are also processed within cacheinfo. Other _cpuid4_info_regs instances are just called "base", adding further ambiguity. Standardize on id4 for all instances. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 97 +++++++++++++++++---------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index b273ecf3f538..1b2a2bf97d7f 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -573,7 +573,7 @@ cache_get_priv_group(struct cacheinfo *ci) return &cache_private_group; } =20 -static void amd_init_l3_cache(struct _cpuid4_info_regs *this_leaf, int ind= ex) +static void amd_init_l3_cache(struct _cpuid4_info_regs *id4, int index) { int node; =20 @@ -582,16 +582,16 @@ static void amd_init_l3_cache(struct _cpuid4_info_reg= s *this_leaf, int index) return; =20 node =3D topology_amd_node_id(smp_processor_id()); - this_leaf->nb =3D node_to_amd_nb(node); - if (this_leaf->nb && !this_leaf->nb->l3_cache.indices) - amd_calc_l3_indices(this_leaf->nb); + id4->nb =3D node_to_amd_nb(node); + if (id4->nb && !id4->nb->l3_cache.indices) + amd_calc_l3_indices(id4->nb); } #else #define amd_init_l3_cache(x, y) #endif /* CONFIG_AMD_NB && CONFIG_SYSFS */ =20 static int -cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *this_leaf) +cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4) { union _cpuid4_leaf_eax eax; union _cpuid4_leaf_ebx ebx; @@ -604,11 +604,11 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_in= fo_regs *this_leaf) &ebx.full, &ecx.full, &edx); else amd_cpuid4(index, &eax, &ebx, &ecx); - amd_init_l3_cache(this_leaf, index); + amd_init_l3_cache(id4, index); } else if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_HYGON) { cpuid_count(0x8000001d, index, &eax.full, &ebx.full, &ecx.full, &edx); - amd_init_l3_cache(this_leaf, index); + amd_init_l3_cache(id4, index); } else { cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx); } @@ -616,13 +616,14 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_in= fo_regs *this_leaf) if (eax.split.type =3D=3D CTYPE_NULL) return -EIO; /* better error ? */ =20 - this_leaf->eax =3D eax; - this_leaf->ebx =3D ebx; - this_leaf->ecx =3D ecx; - this_leaf->size =3D (ecx.split.number_of_sets + 1) * - (ebx.split.coherency_line_size + 1) * - (ebx.split.physical_line_partition + 1) * - (ebx.split.ways_of_associativity + 1); + id4->eax =3D eax; + id4->ebx =3D ebx; + id4->ecx =3D ecx; + id4->size =3D (ecx.split.number_of_sets + 1) * + (ebx.split.coherency_line_size + 1) * + (ebx.split.physical_line_partition + 1) * + (ebx.split.ways_of_associativity + 1); + return 0; } =20 @@ -754,29 +755,29 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) * parameters cpuid leaf to find the cache details */ for (i =3D 0; i < ci->num_leaves; i++) { - struct _cpuid4_info_regs this_leaf =3D {}; + struct _cpuid4_info_regs id4 =3D {}; int retval; =20 - retval =3D cpuid4_cache_lookup_regs(i, &this_leaf); + retval =3D cpuid4_cache_lookup_regs(i, &id4); if (retval < 0) continue; =20 - switch (this_leaf.eax.split.level) { + switch (id4.eax.split.level) { case 1: - if (this_leaf.eax.split.type =3D=3D CTYPE_DATA) - new_l1d =3D this_leaf.size/1024; - else if (this_leaf.eax.split.type =3D=3D CTYPE_INST) - new_l1i =3D this_leaf.size/1024; + if (id4.eax.split.type =3D=3D CTYPE_DATA) + new_l1d =3D id4.size/1024; + else if (id4.eax.split.type =3D=3D CTYPE_INST) + new_l1i =3D id4.size/1024; break; case 2: - new_l2 =3D this_leaf.size/1024; - num_threads_sharing =3D 1 + this_leaf.eax.split.num_threads_sharing; + new_l2 =3D id4.size/1024; + num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; index_msb =3D get_count_order(num_threads_sharing); l2_id =3D c->topo.apicid & ~((1 << index_msb) - 1); break; case 3: - new_l3 =3D this_leaf.size/1024; - num_threads_sharing =3D 1 + this_leaf.eax.split.num_threads_sharing; + new_l3 =3D id4.size/1024; + num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; index_msb =3D get_count_order(num_threads_sharing); l3_id =3D c->topo.apicid & ~((1 << index_msb) - 1); break; @@ -841,7 +842,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) } =20 static int __cache_amd_cpumap_setup(unsigned int cpu, int index, - const struct _cpuid4_info_regs *base) + const struct _cpuid4_info_regs *id4) { struct cpu_cacheinfo *this_cpu_ci; struct cacheinfo *ci; @@ -867,7 +868,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, } else if (boot_cpu_has(X86_FEATURE_TOPOEXT)) { unsigned int apicid, nshared, first, last; =20 - nshared =3D base->eax.split.num_threads_sharing + 1; + nshared =3D id4->eax.split.num_threads_sharing + 1; apicid =3D cpu_data(cpu).topo.apicid; first =3D apicid - (apicid % nshared); last =3D first + nshared - 1; @@ -898,7 +899,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, } =20 static void __cache_cpumap_setup(unsigned int cpu, int index, - const struct _cpuid4_info_regs *base) + const struct _cpuid4_info_regs *id4) { struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci, *sibling_ci; @@ -908,12 +909,12 @@ static void __cache_cpumap_setup(unsigned int cpu, in= t index, =20 if (c->x86_vendor =3D=3D X86_VENDOR_AMD || c->x86_vendor =3D=3D X86_VENDOR_HYGON) { - if (__cache_amd_cpumap_setup(cpu, index, base)) + if (__cache_amd_cpumap_setup(cpu, index, id4)) return; } =20 ci =3D this_cpu_ci->info_list + index; - num_threads_sharing =3D 1 + base->eax.split.num_threads_sharing; + num_threads_sharing =3D 1 + id4->eax.split.num_threads_sharing; =20 cpumask_set_cpu(cpu, &ci->shared_cpu_map); if (num_threads_sharing =3D=3D 1) @@ -934,18 +935,18 @@ static void __cache_cpumap_setup(unsigned int cpu, in= t index, } =20 static void ci_info_init(struct cacheinfo *ci, - const struct _cpuid4_info_regs *base) + const struct _cpuid4_info_regs *id4) { - ci->id =3D base->id; + ci->id =3D id4->id; ci->attributes =3D CACHE_ID; - ci->level =3D base->eax.split.level; - ci->type =3D cache_type_map[base->eax.split.type]; - ci->coherency_line_size =3D base->ebx.split.coherency_line_size + 1; - ci->ways_of_associativity =3D base->ebx.split.ways_of_associativity + 1; - ci->size =3D base->size; - ci->number_of_sets =3D base->ecx.split.number_of_sets + 1; - ci->physical_line_partition =3D base->ebx.split.physical_line_partition += 1; - ci->priv =3D base->nb; + ci->level =3D id4->eax.split.level; + ci->type =3D cache_type_map[id4->eax.split.type]; + ci->coherency_line_size =3D id4->ebx.split.coherency_line_size + 1; + ci->ways_of_associativity =3D id4->ebx.split.ways_of_associativity + 1; + ci->size =3D id4->size; + ci->number_of_sets =3D id4->ecx.split.number_of_sets + 1; + ci->physical_line_partition =3D id4->ebx.split.physical_line_partition + = 1; + ci->priv =3D id4->nb; } =20 int init_cache_level(unsigned int cpu) @@ -964,15 +965,15 @@ int init_cache_level(unsigned int cpu) * ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */ -static void get_cache_id(int cpu, struct _cpuid4_info_regs *id4_regs) +static void get_cache_id(int cpu, struct _cpuid4_info_regs *id4) { struct cpuinfo_x86 *c =3D &cpu_data(cpu); unsigned long num_threads_sharing; int index_msb; =20 - num_threads_sharing =3D 1 + id4_regs->eax.split.num_threads_sharing; + num_threads_sharing =3D 1 + id4->eax.split.num_threads_sharing; index_msb =3D get_count_order(num_threads_sharing); - id4_regs->id =3D c->topo.apicid >> index_msb; + id4->id =3D c->topo.apicid >> index_msb; } =20 int populate_cache_leaves(unsigned int cpu) @@ -980,15 +981,15 @@ int populate_cache_leaves(unsigned int cpu) unsigned int idx, ret; struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci =3D this_cpu_ci->info_list; - struct _cpuid4_info_regs id4_regs =3D {}; + struct _cpuid4_info_regs id4 =3D {}; =20 for (idx =3D 0; idx < this_cpu_ci->num_leaves; idx++) { - ret =3D cpuid4_cache_lookup_regs(idx, &id4_regs); + ret =3D cpuid4_cache_lookup_regs(idx, &id4); if (ret) return ret; - get_cache_id(cpu, &id4_regs); - ci_info_init(ci++, &id4_regs); - __cache_cpumap_setup(cpu, idx, &id4_regs); + get_cache_id(cpu, &id4); + ci_info_init(ci++, &id4); + __cache_cpumap_setup(cpu, idx, &id4); } this_cpu_ci->cpu_map_populated =3D true; =20 --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 EB45E258CCA for ; Wed, 19 Mar 2025 12:22:19 +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=1742386941; cv=none; b=BoPREKOF3yUj4JHELCaXKDzItAboTtvja479DXLjUxjlgrhimQHnkJUR8zmijFJHvn1LVcEn+VcZdYDAFDPiY/sCMiRZetRPfskRxIISgXUwgb2qethcrauPaSmd/rNpHB5ivfhkz4k5EL81xcUb5CQ2eTrU+kzho11DJP3klrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386941; c=relaxed/simple; bh=AZlG9LYByybwrOEfCF+vQCcrCTZi+EZ50a8ylx8Z23g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u/aPCZgGLz2hofGWfZt58g4ATyyE24RHupOmcc2dhck35euFw6Mac+wmtPOQj36vzvVDg8NYOO77ly1DiBGseNaa4185nu4fWzK5Kl0xgdAFyLqC+0Bq4MDr9HdT9T+qAU0hwAQQ1R2gHthnjhGtdS4poiMKlykXdY/1y1zL3dQ= 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=2fav7nYv; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=oUTlJbvp; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2fav7nYv"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oUTlJbvp" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386938; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y30/Lu67ZUIffp0uywi7TFTyLpyoX3QoyFYflTofPOI=; b=2fav7nYvHJ450LG9OHzwoLagj1o/aNkkeWpdSpbwKlDz6aTpflrH+Y6nM91Jqa5izBBWfa bkfwsLihvKVxb91FO0Yj2l6gM+4Kue0dF8zbwrY93zwghulskcKg+MWV4jYwFfkYNFxYAM F0BvxXxUC8w7m8HB1FvFQLbMi/T/mXUnVHQcDiLSZUrlixXSJsnPaoqxJWJAzOlvCHlhHt w7Ge2NGD4Nf7avDVg1JIInF4b5L0ULzdXmrOZlLPsHCpTT5yL5xHiCqMNuafOpVW9Et/F2 seVOajVyQSYNxc8+eOUxpTFMfGr8a6bX5x8kE7sV6IbOUlTTpg3vl0sK79CW7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386938; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y30/Lu67ZUIffp0uywi7TFTyLpyoX3QoyFYflTofPOI=; b=oUTlJbvpxZ3FVEuYxFZzBE7xkPW6FAKAdRxDSMftpv6buTt85q/ppPUGAzlQK5H9MxMu0v pib8Jta8ZnPi8mCw== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 11/29] x86/cacheinfo: Consolidate AMD/Hygon leaf 0x8000001d calls Date: Wed, 19 Mar 2025 13:21:19 +0100 Message-ID: <20250319122137.4004-12-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While gathering CPU cache info, CPUID leaf 0x8000001d is invoked in two separate if blocks: one for Hygon CPUs and one for AMDs with topology extensions. After each invocation, amd_init_l3_cache() is called. Merge the two if blocks into a single condition, thus removing the duplicated code. Future commits will expand these if blocks, so combining them now is both cleaner and more maintainable. Note, while at it, remove a useless "better error?" comment that was within the same function since the 2005 commit e2cac78935ff ("[PATCH] x86_64: When running cpuid4 need to run on the correct CPU"). Note, as previously done at commit aec28d852ed2 ("x86/cpuid: Standardize on u32 in "), standardize on using 'u32' and 'u8' types. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 1b2a2bf97d7f..f1055e806c9f 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -593,28 +593,28 @@ static void amd_init_l3_cache(struct _cpuid4_info_reg= s *id4, int index) static int cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4) { - union _cpuid4_leaf_eax eax; - union _cpuid4_leaf_ebx ebx; - union _cpuid4_leaf_ecx ecx; - unsigned edx; - - if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD) { - if (boot_cpu_has(X86_FEATURE_TOPOEXT)) - cpuid_count(0x8000001d, index, &eax.full, - &ebx.full, &ecx.full, &edx); - else + u8 cpu_vendor =3D boot_cpu_data.x86_vendor; + union _cpuid4_leaf_eax eax; + union _cpuid4_leaf_ebx ebx; + union _cpuid4_leaf_ecx ecx; + u32 edx; + + if (cpu_vendor =3D=3D X86_VENDOR_AMD || cpu_vendor =3D=3D X86_VENDOR_HYGO= N) { + if (boot_cpu_has(X86_FEATURE_TOPOEXT) || cpu_vendor =3D=3D X86_VENDOR_HY= GON) { + /* AMD with TOPOEXT, or HYGON */ + cpuid_count(0x8000001d, index, &eax.full, &ebx.full, &ecx.full, &edx); + } else { + /* Legacy AMD fallback */ amd_cpuid4(index, &eax, &ebx, &ecx); - amd_init_l3_cache(id4, index); - } else if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_HYGON) { - cpuid_count(0x8000001d, index, &eax.full, - &ebx.full, &ecx.full, &edx); + } amd_init_l3_cache(id4, index); } else { + /* Intel */ cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx); } =20 if (eax.split.type =3D=3D CTYPE_NULL) - return -EIO; /* better error ? */ + return -EIO; =20 id4->eax =3D eax; id4->ebx =3D ebx; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 E092B258CDE for ; Wed, 19 Mar 2025 12:22:22 +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=1742386944; cv=none; b=l797nViHtr+ZyCZYITIw8EIeIIOuRD8CATjbMcIxQSOwC1ep77N2iyMlZcuU8mm3pmr3MkS3C/hkpb7vBnv9NeeldZPydqe5Nh9Hd/weM1bhZUBgIc8JgYwLbegGyQDl29UxwvFU0eX9VIKcVo/OpDbPCqGO2KNEZrbeL6U/XZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386944; c=relaxed/simple; bh=Te/nCPJoiBNbyUTWshkZ1ebwFT8DMgtFuf2HdtXmP9o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EYbS1sSlyL/mWW9cz94n9f8QWm4MxP2SIlsIE9DDEaPMolMic75yNeqylKiCb19tE3kdIaPqnE38gcLrIPk3tTiiMu5PQiHNc/9etyUNBpb0yxyqwywBoem0MMHDoZR11y1SpMgdD5nkmIYTV4gGhs9Mdrc9ALiUgHSTNdD5B5E= 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=v4GLQU7J; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=gz7m0NhP; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v4GLQU7J"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="gz7m0NhP" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fDtSqRQzgAIZaXyH6tzIadWLbO0ybWRSQG6EjY6QOvI=; b=v4GLQU7JjvueTNheYF9jW39rbGfj6tApgEN9enkWa3LAVOBTY1avkWH5bNSJm4hXia12OO Bfzu08p3rhhzP7QiVSHnavZhmQvC9NX5/KL23xG8kqaX8l080sUl+uJh/gwh01XbZJgceO a4sjf3QNJfbOqOjZllDam2yjSdfDrHuP1S2dFmHyEobWSJmmF2d94GvqSK6LgcyFKA8SjQ qAthc7W9JPQRdn4zUjUQ3JmtOMYj39wjO0e0zptrtmaHvpyPvBtT6Lg/IoO4eRi6el67yA FuPs2j0+7VUF48UPnpFzrzhEb6qm4mW3QnM0H1MQYbr9rlvMnmZqAH4hdqATFw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fDtSqRQzgAIZaXyH6tzIadWLbO0ybWRSQG6EjY6QOvI=; b=gz7m0NhP51cmkKftabjBc01Dqm7sr24giKdiEyavFRG+XCn88Cnd90JSxvT7/7GOLYdFEc s40EwAW+ETrkTACg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 12/29] x86/cacheinfo: Separate amd_northbridge from _cpuid4_info_regs Date: Wed, 19 Mar 2025 13:21:20 +0100 Message-ID: <20250319122137.4004-13-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The _cpuid4_info_regs structure is meant to hold the CPUID leaf 0x4 output registers (EAX, EBX, and ECX), as well as derived information such as the cache node ID and size. It also contains a reference to amd_northbridge, which is there only to be "parked" until ci_info_init() can store it in the priv pointer of the API. That priv pointer is then used by AMD-specific L3 cache_disable_0/1 sysfs attributes. Decouple amd_northbridge from _cpuid4_info_regs and pass it explicitly through the functions at x86/cacheinfo. Doing so clarifies when amd_northbridge is actually needed (AMD-only code) and when it is not (Intel-specific code). It also prepares for moving the AMD-specific L3 cache_disable_0/1 sysfs code into its own file in next commit. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 45 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index f1055e806c9f..8c2b51bb032a 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -168,7 +168,6 @@ struct _cpuid4_info_regs { union _cpuid4_leaf_ecx ecx; unsigned int id; unsigned long size; - struct amd_northbridge *nb; }; =20 /* AMD doesn't have CPUID4. Emulate it here to report the same @@ -573,25 +572,36 @@ cache_get_priv_group(struct cacheinfo *ci) return &cache_private_group; } =20 -static void amd_init_l3_cache(struct _cpuid4_info_regs *id4, int index) +static struct amd_northbridge *amd_init_l3_cache(int index) { + struct amd_northbridge *nb; int node; =20 /* only for L3, and not in virtualized environments */ if (index < 3) - return; + return NULL; =20 node =3D topology_amd_node_id(smp_processor_id()); - id4->nb =3D node_to_amd_nb(node); - if (id4->nb && !id4->nb->l3_cache.indices) - amd_calc_l3_indices(id4->nb); + nb =3D node_to_amd_nb(node); + if (nb && !nb->l3_cache.indices) + amd_calc_l3_indices(nb); + + return nb; } #else -#define amd_init_l3_cache(x, y) +static struct amd_northbridge *amd_init_l3_cache(int index) +{ + return NULL; +} #endif /* CONFIG_AMD_NB && CONFIG_SYSFS */ =20 -static int -cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4) +/* + * Fill passed _cpuid4_info_regs structure. + * Intel-only code paths should pass NULL for the amd_northbridge + * return pointer. + */ +static int cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *i= d4, + struct amd_northbridge **nb) { u8 cpu_vendor =3D boot_cpu_data.x86_vendor; union _cpuid4_leaf_eax eax; @@ -607,7 +617,9 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_info= _regs *id4) /* Legacy AMD fallback */ amd_cpuid4(index, &eax, &ebx, &ecx); } - amd_init_l3_cache(id4, index); + + if (nb) + *nb =3D amd_init_l3_cache(index); } else { /* Intel */ cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx); @@ -758,7 +770,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) struct _cpuid4_info_regs id4 =3D {}; int retval; =20 - retval =3D cpuid4_cache_lookup_regs(i, &id4); + retval =3D cpuid4_cache_lookup_regs(i, &id4, NULL); if (retval < 0) continue; =20 @@ -934,8 +946,8 @@ static void __cache_cpumap_setup(unsigned int cpu, int = index, } } =20 -static void ci_info_init(struct cacheinfo *ci, - const struct _cpuid4_info_regs *id4) +static void ci_info_init(struct cacheinfo *ci, const struct _cpuid4_info_r= egs *id4, + struct amd_northbridge *nb) { ci->id =3D id4->id; ci->attributes =3D CACHE_ID; @@ -946,7 +958,7 @@ static void ci_info_init(struct cacheinfo *ci, ci->size =3D id4->size; ci->number_of_sets =3D id4->ecx.split.number_of_sets + 1; ci->physical_line_partition =3D id4->ebx.split.physical_line_partition + = 1; - ci->priv =3D id4->nb; + ci->priv =3D nb; } =20 int init_cache_level(unsigned int cpu) @@ -982,13 +994,14 @@ int populate_cache_leaves(unsigned int cpu) struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci =3D this_cpu_ci->info_list; struct _cpuid4_info_regs id4 =3D {}; + struct amd_northbridge *nb; =20 for (idx =3D 0; idx < this_cpu_ci->num_leaves; idx++) { - ret =3D cpuid4_cache_lookup_regs(idx, &id4); + ret =3D cpuid4_cache_lookup_regs(idx, &id4, &nb); if (ret) return ret; get_cache_id(cpu, &id4); - ci_info_init(ci++, &id4); + ci_info_init(ci++, &id4, nb); __cache_cpumap_setup(cpu, idx, &id4); } this_cpu_ci->cpu_map_populated =3D true; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 511882594B4 for ; Wed, 19 Mar 2025 12:22:26 +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=1742386948; cv=none; b=F1n2GNv78JrTHS07+iyMmqG+samGdVgbLsWSSlHgJF1c2CwvQGWRRLkaB/eQSrMCTZuTqdsEuOUP1KQixPaYjWaco8iHaNtf/DZIKT6LXZjDzM5Dj8TeOuEGEKBDaHr4yAA8AyBBVKfL7rJBw1gd17a9RLDfzRFZGpPif8mD8Lo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386948; c=relaxed/simple; bh=xTYhXhf1xMusTJNR30yuEF+tG8dRBcUdieR2S9ch1Zc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oVVUCLBkgjEigEZ/UiQXub6uz8QaKwiRJQ85qi2GVED0hYUd2u9hIzXO+pam2up8RuWsjtNQdGy9owa5G6v0hcdUYyNfO4oU21Tbw95rMtck/ta1xEuqAGZVR3D+c1RdBmlvh9FeNJ4OCkNJpIWJz1VIn0ZC/kBWoC4gOmQoXDc= 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=v91g4gXX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZsuOXL+T; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v91g4gXX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZsuOXL+T" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386944; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fTOb6sHWAsrfXPNZ4GtJUHlC3sbGwZMqTn57GX36Tp4=; b=v91g4gXXkQfBVPneKiGCFZ1FNE7dnEsDXIHABVvMtF0QtsEA1/LCL6vf5ETb5r/7eu6lkH h9v31xa57BOue0yRptFokt1ZmRXNme4cHWiNoIs0dYz5fnBxgJYFPcUjXASGFJqelF9lpi F9xL5aCo7zFiIeOdgmKpM4YXOpDS8/X5CZhqMEj2+ERsRukdrHM7yNj32wnrX3yiFRF6aT L6jYE4Mn91J4gAF4LxPbdoXmE9b5k0z1yb8Xw7t383Gre1absCW3yB3nJdEytkMyIcFv1y PUFJmGQ+BxUWwP8B9uBB9Aktf1zKhDRcTE3SBjv6lfMNP/iU1SxjaEr5ELe13Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386944; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fTOb6sHWAsrfXPNZ4GtJUHlC3sbGwZMqTn57GX36Tp4=; b=ZsuOXL+T9O4QtX+jRFAf/lVUoC/FpVZAerE4IVxUySfvnShr4vPzWMCijHb35hgfKX5iFs yF2CPz0XZPalQ5DQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 13/29] x86/cacheinfo: Move AMD cache_disable_0/1 handling to separate file Date: Wed, 19 Mar 2025 13:21:21 +0100 Message-ID: <20250319122137.4004-14-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Parent commit decoupled amd_northbridge out of _cpuid4_info_regs, where it was merely "parked" there until ci_info_init() can store it in the private pointer of the API. Given that decoupling, move the AMD-specific L3 cache_disable_0/1 sysfs code from the generic (and already extremely convoluted) x86/cacheinfo code into its own file. Compile the file only if CONFIG_AMD_NB and CONFIG_SYSFS are both enabled, which mirrors the existing logic. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/Makefile | 3 + arch/x86/kernel/cpu/amd_cache_disable.c | 301 ++++++++++++++++++++++++ arch/x86/kernel/cpu/cacheinfo.c | 298 ----------------------- arch/x86/kernel/cpu/cpu.h | 9 + 4 files changed, 313 insertions(+), 298 deletions(-) create mode 100644 arch/x86/kernel/cpu/amd_cache_disable.c diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile index 4efdf5c2efc8..3a39396d422d 100644 --- a/arch/x86/kernel/cpu/Makefile +++ b/arch/x86/kernel/cpu/Makefile @@ -38,6 +38,9 @@ obj-y +=3D intel.o tsx.o obj-$(CONFIG_PM) +=3D intel_epb.o endif obj-$(CONFIG_CPU_SUP_AMD) +=3D amd.o +ifeq ($(CONFIG_AMD_NB)$(CONFIG_SYSFS),yy) +obj-y +=3D amd_cache_disable.o +endif obj-$(CONFIG_CPU_SUP_HYGON) +=3D hygon.o obj-$(CONFIG_CPU_SUP_CYRIX_32) +=3D cyrix.o obj-$(CONFIG_CPU_SUP_CENTAUR) +=3D centaur.o diff --git a/arch/x86/kernel/cpu/amd_cache_disable.c b/arch/x86/kernel/cpu/= amd_cache_disable.c new file mode 100644 index 000000000000..6d53aee0d869 --- /dev/null +++ b/arch/x86/kernel/cpu/amd_cache_disable.c @@ -0,0 +1,301 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * AMD L3 cache_disable_{0,1} sysfs handling + * Documentation/ABI/testing/sysfs-devices-system-cpu + */ + +#include +#include +#include +#include + +#include + +#include "cpu.h" + +/* + * L3 cache descriptors + */ +static void amd_calc_l3_indices(struct amd_northbridge *nb) +{ + struct amd_l3_cache *l3 =3D &nb->l3_cache; + unsigned int sc0, sc1, sc2, sc3; + u32 val =3D 0; + + pci_read_config_dword(nb->misc, 0x1C4, &val); + + /* calculate subcache sizes */ + l3->subcaches[0] =3D sc0 =3D !(val & BIT(0)); + l3->subcaches[1] =3D sc1 =3D !(val & BIT(4)); + + if (boot_cpu_data.x86 =3D=3D 0x15) { + l3->subcaches[0] =3D sc0 +=3D !(val & BIT(1)); + l3->subcaches[1] =3D sc1 +=3D !(val & BIT(5)); + } + + l3->subcaches[2] =3D sc2 =3D !(val & BIT(8)) + !(val & BIT(9)); + l3->subcaches[3] =3D sc3 =3D !(val & BIT(12)) + !(val & BIT(13)); + + l3->indices =3D (max(max3(sc0, sc1, sc2), sc3) << 10) - 1; +} + +/* + * check whether a slot used for disabling an L3 index is occupied. + * @l3: L3 cache descriptor + * @slot: slot number (0..1) + * + * @returns: the disabled index if used or negative value if slot free. + */ +static int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned in= t slot) +{ + unsigned int reg =3D 0; + + pci_read_config_dword(nb->misc, 0x1BC + slot * 4, ®); + + /* check whether this slot is activated already */ + if (reg & (3UL << 30)) + return reg & 0xfff; + + return -1; +} + +static ssize_t show_cache_disable(struct cacheinfo *ci, char *buf, unsigne= d int slot) +{ + int index; + struct amd_northbridge *nb =3D ci->priv; + + index =3D amd_get_l3_disable_slot(nb, slot); + if (index >=3D 0) + return sprintf(buf, "%d\n", index); + + return sprintf(buf, "FREE\n"); +} + +#define SHOW_CACHE_DISABLE(slot) \ +static ssize_t \ +cache_disable_##slot##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct cacheinfo *ci =3D dev_get_drvdata(dev); \ + return show_cache_disable(ci, buf, slot); \ +} + +SHOW_CACHE_DISABLE(0) +SHOW_CACHE_DISABLE(1) + +static void amd_l3_disable_index(struct amd_northbridge *nb, int cpu, + unsigned int slot, unsigned long idx) +{ + int i; + + idx |=3D BIT(30); + + /* + * disable index in all 4 subcaches + */ + for (i =3D 0; i < 4; i++) { + u32 reg =3D idx | (i << 20); + + if (!nb->l3_cache.subcaches[i]) + continue; + + pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg); + + /* + * We need to WBINVD on a core on the node containing the L3 + * cache which indices we disable therefore a simple wbinvd() + * is not sufficient. + */ + wbinvd_on_cpu(cpu); + + reg |=3D BIT(31); + pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg); + } +} + +/* + * disable a L3 cache index by using a disable-slot + * + * @l3: L3 cache descriptor + * @cpu: A CPU on the node containing the L3 cache + * @slot: slot number (0..1) + * @index: index to disable + * + * @return: 0 on success, error status on failure + */ +static int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu, + unsigned int slot, unsigned long index) +{ + int ret =3D 0; + + /* check if @slot is already used or the index is already disabled */ + ret =3D amd_get_l3_disable_slot(nb, slot); + if (ret >=3D 0) + return -EEXIST; + + if (index > nb->l3_cache.indices) + return -EINVAL; + + /* check whether the other slot has disabled the same index already */ + if (index =3D=3D amd_get_l3_disable_slot(nb, !slot)) + return -EEXIST; + + amd_l3_disable_index(nb, cpu, slot, index); + + return 0; +} + +static ssize_t store_cache_disable(struct cacheinfo *ci, const char *buf, + size_t count, unsigned int slot) +{ + struct amd_northbridge *nb =3D ci->priv; + unsigned long val =3D 0; + int cpu, err =3D 0; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + cpu =3D cpumask_first(&ci->shared_cpu_map); + + if (kstrtoul(buf, 10, &val) < 0) + return -EINVAL; + + err =3D amd_set_l3_disable_slot(nb, cpu, slot, val); + if (err) { + if (err =3D=3D -EEXIST) + pr_warn("L3 slot %d in use/index already disabled!\n", + slot); + return err; + } + return count; +} + +#define STORE_CACHE_DISABLE(slot) \ +static ssize_t \ +cache_disable_##slot##_store(struct device *dev, \ + struct device_attribute *attr, \ + const char *buf, size_t count) \ +{ \ + struct cacheinfo *ci =3D dev_get_drvdata(dev); \ + return store_cache_disable(ci, buf, count, slot); \ +} + +STORE_CACHE_DISABLE(0) +STORE_CACHE_DISABLE(1) + +static ssize_t subcaches_show(struct device *dev, struct device_attribute = *attr, + char *buf) +{ + struct cacheinfo *ci =3D dev_get_drvdata(dev); + int cpu =3D cpumask_first(&ci->shared_cpu_map); + + return sprintf(buf, "%x\n", amd_get_subcaches(cpu)); +} + +static ssize_t subcaches_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct cacheinfo *ci =3D dev_get_drvdata(dev); + int cpu =3D cpumask_first(&ci->shared_cpu_map); + unsigned long val; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + if (kstrtoul(buf, 16, &val) < 0) + return -EINVAL; + + if (amd_set_subcaches(cpu, val)) + return -EINVAL; + + return count; +} + +static DEVICE_ATTR_RW(cache_disable_0); +static DEVICE_ATTR_RW(cache_disable_1); +static DEVICE_ATTR_RW(subcaches); + +static umode_t cache_private_attrs_is_visible(struct kobject *kobj, + struct attribute *attr, int unused) +{ + struct device *dev =3D kobj_to_dev(kobj); + struct cacheinfo *ci =3D dev_get_drvdata(dev); + umode_t mode =3D attr->mode; + + if (!ci->priv) + return 0; + + if ((attr =3D=3D &dev_attr_subcaches.attr) && + amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) + return mode; + + if ((attr =3D=3D &dev_attr_cache_disable_0.attr || + attr =3D=3D &dev_attr_cache_disable_1.attr) && + amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) + return mode; + + return 0; +} + +static struct attribute_group cache_private_group =3D { + .is_visible =3D cache_private_attrs_is_visible, +}; + +static void init_amd_l3_attrs(void) +{ + static struct attribute **amd_l3_attrs; + int n =3D 1; + + if (amd_l3_attrs) /* already initialized */ + return; + + if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) + n +=3D 2; + if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) + n +=3D 1; + + amd_l3_attrs =3D kcalloc(n, sizeof(*amd_l3_attrs), GFP_KERNEL); + if (!amd_l3_attrs) + return; + + n =3D 0; + if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) { + amd_l3_attrs[n++] =3D &dev_attr_cache_disable_0.attr; + amd_l3_attrs[n++] =3D &dev_attr_cache_disable_1.attr; + } + if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) + amd_l3_attrs[n++] =3D &dev_attr_subcaches.attr; + + cache_private_group.attrs =3D amd_l3_attrs; +} + +const struct attribute_group *cache_get_priv_group(struct cacheinfo *ci) +{ + struct amd_northbridge *nb =3D ci->priv; + + if (ci->level < 3 || !nb) + return NULL; + + if (nb && nb->l3_cache.indices) + init_amd_l3_attrs(); + + return &cache_private_group; +} + +struct amd_northbridge *amd_init_l3_cache(int index) +{ + struct amd_northbridge *nb; + int node; + + /* only for L3, and not in virtualized environments */ + if (index < 3) + return NULL; + + node =3D topology_amd_node_id(smp_processor_id()); + nb =3D node_to_amd_nb(node); + if (nb && !nb->l3_cache.indices) + amd_calc_l3_indices(nb); + + return nb; +} diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 8c2b51bb032a..ea6fba942d27 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -9,12 +9,9 @@ */ =20 #include -#include #include #include -#include #include -#include =20 #include #include @@ -300,301 +297,6 @@ amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, (ebx->split.ways_of_associativity + 1) - 1; } =20 -#if defined(CONFIG_AMD_NB) && defined(CONFIG_SYSFS) - -/* - * L3 cache descriptors - */ -static void amd_calc_l3_indices(struct amd_northbridge *nb) -{ - struct amd_l3_cache *l3 =3D &nb->l3_cache; - unsigned int sc0, sc1, sc2, sc3; - u32 val =3D 0; - - pci_read_config_dword(nb->misc, 0x1C4, &val); - - /* calculate subcache sizes */ - l3->subcaches[0] =3D sc0 =3D !(val & BIT(0)); - l3->subcaches[1] =3D sc1 =3D !(val & BIT(4)); - - if (boot_cpu_data.x86 =3D=3D 0x15) { - l3->subcaches[0] =3D sc0 +=3D !(val & BIT(1)); - l3->subcaches[1] =3D sc1 +=3D !(val & BIT(5)); - } - - l3->subcaches[2] =3D sc2 =3D !(val & BIT(8)) + !(val & BIT(9)); - l3->subcaches[3] =3D sc3 =3D !(val & BIT(12)) + !(val & BIT(13)); - - l3->indices =3D (max(max3(sc0, sc1, sc2), sc3) << 10) - 1; -} - -/* - * check whether a slot used for disabling an L3 index is occupied. - * @l3: L3 cache descriptor - * @slot: slot number (0..1) - * - * @returns: the disabled index if used or negative value if slot free. - */ -static int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned sl= ot) -{ - unsigned int reg =3D 0; - - pci_read_config_dword(nb->misc, 0x1BC + slot * 4, ®); - - /* check whether this slot is activated already */ - if (reg & (3UL << 30)) - return reg & 0xfff; - - return -1; -} - -static ssize_t show_cache_disable(struct cacheinfo *ci, char *buf, unsigne= d int slot) -{ - int index; - struct amd_northbridge *nb =3D ci->priv; - - index =3D amd_get_l3_disable_slot(nb, slot); - if (index >=3D 0) - return sprintf(buf, "%d\n", index); - - return sprintf(buf, "FREE\n"); -} - -#define SHOW_CACHE_DISABLE(slot) \ -static ssize_t \ -cache_disable_##slot##_show(struct device *dev, \ - struct device_attribute *attr, char *buf) \ -{ \ - struct cacheinfo *ci =3D dev_get_drvdata(dev); \ - return show_cache_disable(ci, buf, slot); \ -} -SHOW_CACHE_DISABLE(0) -SHOW_CACHE_DISABLE(1) - -static void amd_l3_disable_index(struct amd_northbridge *nb, int cpu, - unsigned slot, unsigned long idx) -{ - int i; - - idx |=3D BIT(30); - - /* - * disable index in all 4 subcaches - */ - for (i =3D 0; i < 4; i++) { - u32 reg =3D idx | (i << 20); - - if (!nb->l3_cache.subcaches[i]) - continue; - - pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg); - - /* - * We need to WBINVD on a core on the node containing the L3 - * cache which indices we disable therefore a simple wbinvd() - * is not sufficient. - */ - wbinvd_on_cpu(cpu); - - reg |=3D BIT(31); - pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg); - } -} - -/* - * disable a L3 cache index by using a disable-slot - * - * @l3: L3 cache descriptor - * @cpu: A CPU on the node containing the L3 cache - * @slot: slot number (0..1) - * @index: index to disable - * - * @return: 0 on success, error status on failure - */ -static int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu, - unsigned slot, unsigned long index) -{ - int ret =3D 0; - - /* check if @slot is already used or the index is already disabled */ - ret =3D amd_get_l3_disable_slot(nb, slot); - if (ret >=3D 0) - return -EEXIST; - - if (index > nb->l3_cache.indices) - return -EINVAL; - - /* check whether the other slot has disabled the same index already */ - if (index =3D=3D amd_get_l3_disable_slot(nb, !slot)) - return -EEXIST; - - amd_l3_disable_index(nb, cpu, slot, index); - - return 0; -} - -static ssize_t store_cache_disable(struct cacheinfo *ci, const char *buf, - size_t count, unsigned int slot) -{ - unsigned long val =3D 0; - int cpu, err =3D 0; - struct amd_northbridge *nb =3D ci->priv; - - if (!capable(CAP_SYS_ADMIN)) - return -EPERM; - - cpu =3D cpumask_first(&ci->shared_cpu_map); - - if (kstrtoul(buf, 10, &val) < 0) - return -EINVAL; - - err =3D amd_set_l3_disable_slot(nb, cpu, slot, val); - if (err) { - if (err =3D=3D -EEXIST) - pr_warn("L3 slot %d in use/index already disabled!\n", - slot); - return err; - } - return count; -} - -#define STORE_CACHE_DISABLE(slot) \ -static ssize_t \ -cache_disable_##slot##_store(struct device *dev, \ - struct device_attribute *attr, \ - const char *buf, size_t count) \ -{ \ - struct cacheinfo *ci =3D dev_get_drvdata(dev); \ - return store_cache_disable(ci, buf, count, slot); \ -} -STORE_CACHE_DISABLE(0) -STORE_CACHE_DISABLE(1) - -static ssize_t subcaches_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct cacheinfo *ci =3D dev_get_drvdata(dev); - int cpu =3D cpumask_first(&ci->shared_cpu_map); - - return sprintf(buf, "%x\n", amd_get_subcaches(cpu)); -} - -static ssize_t subcaches_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct cacheinfo *ci =3D dev_get_drvdata(dev); - int cpu =3D cpumask_first(&ci->shared_cpu_map); - unsigned long val; - - if (!capable(CAP_SYS_ADMIN)) - return -EPERM; - - if (kstrtoul(buf, 16, &val) < 0) - return -EINVAL; - - if (amd_set_subcaches(cpu, val)) - return -EINVAL; - - return count; -} - -static DEVICE_ATTR_RW(cache_disable_0); -static DEVICE_ATTR_RW(cache_disable_1); -static DEVICE_ATTR_RW(subcaches); - -static umode_t -cache_private_attrs_is_visible(struct kobject *kobj, - struct attribute *attr, int unused) -{ - struct device *dev =3D kobj_to_dev(kobj); - struct cacheinfo *ci =3D dev_get_drvdata(dev); - umode_t mode =3D attr->mode; - - if (!ci->priv) - return 0; - - if ((attr =3D=3D &dev_attr_subcaches.attr) && - amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) - return mode; - - if ((attr =3D=3D &dev_attr_cache_disable_0.attr || - attr =3D=3D &dev_attr_cache_disable_1.attr) && - amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) - return mode; - - return 0; -} - -static struct attribute_group cache_private_group =3D { - .is_visible =3D cache_private_attrs_is_visible, -}; - -static void init_amd_l3_attrs(void) -{ - int n =3D 1; - static struct attribute **amd_l3_attrs; - - if (amd_l3_attrs) /* already initialized */ - return; - - if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) - n +=3D 2; - if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) - n +=3D 1; - - amd_l3_attrs =3D kcalloc(n, sizeof(*amd_l3_attrs), GFP_KERNEL); - if (!amd_l3_attrs) - return; - - n =3D 0; - if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) { - amd_l3_attrs[n++] =3D &dev_attr_cache_disable_0.attr; - amd_l3_attrs[n++] =3D &dev_attr_cache_disable_1.attr; - } - if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING)) - amd_l3_attrs[n++] =3D &dev_attr_subcaches.attr; - - cache_private_group.attrs =3D amd_l3_attrs; -} - -const struct attribute_group * -cache_get_priv_group(struct cacheinfo *ci) -{ - struct amd_northbridge *nb =3D ci->priv; - - if (ci->level < 3 || !nb) - return NULL; - - if (nb && nb->l3_cache.indices) - init_amd_l3_attrs(); - - return &cache_private_group; -} - -static struct amd_northbridge *amd_init_l3_cache(int index) -{ - struct amd_northbridge *nb; - int node; - - /* only for L3, and not in virtualized environments */ - if (index < 3) - return NULL; - - node =3D topology_amd_node_id(smp_processor_id()); - nb =3D node_to_amd_nb(node); - if (nb && !nb->l3_cache.indices) - amd_calc_l3_indices(nb); - - return nb; -} -#else -static struct amd_northbridge *amd_init_l3_cache(int index) -{ - return NULL; -} -#endif /* CONFIG_AMD_NB && CONFIG_SYSFS */ - /* * Fill passed _cpuid4_info_regs structure. * Intel-only code paths should pass NULL for the amd_northbridge diff --git a/arch/x86/kernel/cpu/cpu.h b/arch/x86/kernel/cpu/cpu.h index 51deb60a9d26..bc38b2d56f26 100644 --- a/arch/x86/kernel/cpu/cpu.h +++ b/arch/x86/kernel/cpu/cpu.h @@ -75,6 +75,15 @@ extern void check_null_seg_clears_base(struct cpuinfo_x8= 6 *c); void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u16 die_id); void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c); =20 +#if defined(CONFIG_AMD_NB) && defined(CONFIG_SYSFS) +struct amd_northbridge *amd_init_l3_cache(int index); +#else +static inline struct amd_northbridge *amd_init_l3_cache(int index) +{ + return NULL; +} +#endif + unsigned int aperfmperf_get_khz(int cpu); void cpu_select_mitigations(void); =20 --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 089832561BE for ; Wed, 19 Mar 2025 12:22:29 +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=1742386950; cv=none; b=ilMnsHQfbGLUu0F6RN0QtAw8xT6+af1Z4OdCtFpb/9gliShtaGry+kDJ+VaCd0sK7z8+HVthjcGMLYJMsV+ud4zFXiOt9M9vjIFP/41nXpBuFfDHbAlEAjE362ph9aeqH69v7flW+Gj9pjWcBPzBHAg0xw74ziTdrGjtus73RrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386950; c=relaxed/simple; bh=wEw51A1Z/Kflsa07gSPSJJT92gdgpGM02PW8kbXEZCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YksMcjmeRpDjkklhPTzP8Um4BgkSeLVb1I2TMKsoMp2kvkZ6UPOs3RkppnMNp45E9G26Mz3VLi44Eb2cSeIXpNelYq6OYFP8ZweSMcOVU+EWLdSTqArvRvPUZkTuSSDjp5JxBLCr0MVVmShnW/hAkzzTcHROE1ig2vS+rnTgWCs= 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=1AqllIAc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=L/4D+FMV; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1AqllIAc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="L/4D+FMV" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386947; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wIM1/UO/olTqtQhk58W5MQceA/7wLYg5WiO97m4eSsc=; b=1AqllIAcsCbesq3Wd/wJHiUQkqUbWtWRP90jFfFD4T2Ggt6G7PDtzdqEhnsOm8bnk4ABpU J2X+xYWSqwpTU22fPk3jqYxx8puo9cxZ1OO3/1wRwrOpzD+8LAaA8kjkDS/HCL5QxpqilV G3g7d+DfPvdxUGq+Y0JkCa2oODsRRyBHyrzOIM1E54g1f1O/rbsqcInbTpUJCiSULtVy3W l2Ua9xr6waVgmzLRvZtEQka29sUymH0olp7KtNl95lrYHXDooKmpoxQFFE9sWR0afFSmJN rwa5OW7WVubsF3nnJpIWgisCRdyo9PDiAT5Q9irjj01zANqdPTzlXZojES6SUQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386947; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wIM1/UO/olTqtQhk58W5MQceA/7wLYg5WiO97m4eSsc=; b=L/4D+FMV/qTZF1E7vlWMHcEmL9Uc4jAQXmVb3YRRY6ym0M3fOQkqksee0t7lCp/EiDt/0K al3E4xu2KWLEltAg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 14/29] x86/cacheinfo: Use sysfs_emit() for sysfs attributes show() Date: Wed, 19 Mar 2025 13:21:22 +0100 Message-ID: <20250319122137.4004-15-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Per Documentation/filesystems/sysfs.rst, a sysfs attribute's show() method should only use sysfs_emit() or sysfs_emit_at() when returning values to user space. Use sysfs_emit() for the AMD L3 cache sysfs attributes cache_disable_0, cache_disable_1, and subcaches. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/amd_cache_disable.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/cpu/amd_cache_disable.c b/arch/x86/kernel/cpu/= amd_cache_disable.c index 6d53aee0d869..d860ad3f8a5a 100644 --- a/arch/x86/kernel/cpu/amd_cache_disable.c +++ b/arch/x86/kernel/cpu/amd_cache_disable.c @@ -66,9 +66,9 @@ static ssize_t show_cache_disable(struct cacheinfo *ci, c= har *buf, unsigned int =20 index =3D amd_get_l3_disable_slot(nb, slot); if (index >=3D 0) - return sprintf(buf, "%d\n", index); + return sysfs_emit(buf, "%d\n", index); =20 - return sprintf(buf, "FREE\n"); + return sysfs_emit(buf, "FREE\n"); } =20 #define SHOW_CACHE_DISABLE(slot) \ @@ -189,7 +189,7 @@ static ssize_t subcaches_show(struct device *dev, struc= t device_attribute *attr, struct cacheinfo *ci =3D dev_get_drvdata(dev); int cpu =3D cpumask_first(&ci->shared_cpu_map); =20 - return sprintf(buf, "%x\n", amd_get_subcaches(cpu)); + return sysfs_emit(buf, "%x\n", amd_get_subcaches(cpu)); } =20 static ssize_t subcaches_store(struct device *dev, --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 31818256C94 for ; Wed, 19 Mar 2025 12:22:32 +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=1742386954; cv=none; b=IsukFHzJuljza/qUVc2RHP8LzSaWmnIILwJp0Vlp1u2DV4FZmxz9Kwhe/P6bZgVs1R4vAnFK9K0GLJxAiP7PFFrm9Y3QHaY//CPuTBNmLNGB5ab2pWDBG7QqkV2FNnJJniYMkCDmUPZtG5R/c75H7MHINxdqZWrubqqu05lGMRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386954; c=relaxed/simple; bh=Bjtsf7lg8EHicKEMmbJfmtIdAuqMzZKYuPZta9u7Auo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D5aU3MiO7jy8VpJvX2G0snSvnoycsN41sMl1EKA6YBpyEnDmFn5kG4kQK3jk0hOqV9sqAdrEi62Qd/o8bl1jmLL7lYXOBFqNBGnINoqtVES1gWTD8Haf0GtANDKGejRd/YUDezP2rUYnRTOYyX7sq/A/40KW0/UkQWIiYcWxG4Q= 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=CZd1XHY+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rfOlGDGZ; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CZd1XHY+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rfOlGDGZ" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386950; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YDndO82aVgh8tOwhp/eC/3rwOEZ9NJG0L96Sv7FsXQA=; b=CZd1XHY+ruclji2YYXH9IqvWPhs6AHWByvZEB2hsVCzYwkV/GGMYwpYMMJSqvV+IixqhSP nftrTdDSsql651+/f5io9xo+BT3/ENvaD5/atf/3qMEEbUJedqdXg9x6yikoGnXubP8TZe 3mU7JZWPoYzIXaR+F7yAwrR2PJsMHJvX/LndGu7WT6FLSJOOlpbAIr/P5XVx0YlAkYOyTu Vksi3Ee2Ew7xOuIBgZksgA99bHqSilDkhstku1f/MQEJ6TBoFD0lne4L4tKkZR15l9pv/P 7pGjQbDvH/FuwCfnXZxV+XMF3SzMbnx+7lzeyJLk2Q413whEHyOF/5wo9WSK/w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386950; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YDndO82aVgh8tOwhp/eC/3rwOEZ9NJG0L96Sv7FsXQA=; b=rfOlGDGZKDGcEFLCQcFUwY2Qh1ZZFQLFqmDtZ+RkisC/tcnYV62VKuCKCgeOuphY/WHFky u4HErJd4IfgHVQCA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 15/29] x86/cacheinfo: Separate Intel and AMD leaf 0x4 code paths Date: Wed, 19 Mar 2025 13:21:23 +0100 Message-ID: <20250319122137.4004-16-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The leaf 0x4 parsing code at cpuid4_cache_lookup_regs() is ugly and convoluted. It is tangled with multiple nested conditions to handle: * AMD with TOPEXT, or Hygon CPUs via leaf 0x8000001d * Legacy AMD fallback via leaf 0x4 emulation * Intel CPUs via the actual CPUID leaf 0x4 Moreover, AMD L3 northbridge initialization is also awkwardly placed alongside the CPUID calls of the first two scenarios above. Refactor all of that as follows: * Update AMD's leaf 0x4 emulation comment to represent current state * Clearly label the AMD leaf 0x4 emulation function as a fallback * Split AMD/Hygon and Intel code paths into separate functions * Move AMD L3 northbridge initialization out of CPUID leaf 0x4 code, and into populate_cache_leaves() where it belongs. There, ci_info_init() can directly store the initialized object in the private pointer of the API. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 95 +++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index ea6fba942d27..10a79d87ce86 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -167,12 +167,11 @@ struct _cpuid4_info_regs { unsigned long size; }; =20 -/* AMD doesn't have CPUID4. Emulate it here to report the same - information to the user. This makes some assumptions about the machine: - L2 not shared, no SMT etc. that is currently true on AMD CPUs. +/* + * Fallback AMD CPUID(4) emulation + * AMD CPUs with TOPOEXT can just use CPUID(0x8000001d) + */ =20 - In theory the TLBs could be reported as fake type (they are in "dummy"). - Maybe later */ union l1_cache { struct { unsigned line_size:8; @@ -228,9 +227,8 @@ static const enum cache_type cache_type_map[] =3D { [CTYPE_UNIFIED] =3D CACHE_TYPE_UNIFIED, }; =20 -static void -amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, - union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx) +static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, + union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx) { unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb; union l1_cache l1i, l1d; @@ -297,36 +295,9 @@ amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, (ebx->split.ways_of_associativity + 1) - 1; } =20 -/* - * Fill passed _cpuid4_info_regs structure. - * Intel-only code paths should pass NULL for the amd_northbridge - * return pointer. - */ -static int cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *i= d4, - struct amd_northbridge **nb) +static int cpuid4_info_fill_done(struct _cpuid4_info_regs *id4, union _cpu= id4_leaf_eax eax, + union _cpuid4_leaf_ebx ebx, union _cpuid4_leaf_ecx ecx) { - u8 cpu_vendor =3D boot_cpu_data.x86_vendor; - union _cpuid4_leaf_eax eax; - union _cpuid4_leaf_ebx ebx; - union _cpuid4_leaf_ecx ecx; - u32 edx; - - if (cpu_vendor =3D=3D X86_VENDOR_AMD || cpu_vendor =3D=3D X86_VENDOR_HYGO= N) { - if (boot_cpu_has(X86_FEATURE_TOPOEXT) || cpu_vendor =3D=3D X86_VENDOR_HY= GON) { - /* AMD with TOPOEXT, or HYGON */ - cpuid_count(0x8000001d, index, &eax.full, &ebx.full, &ecx.full, &edx); - } else { - /* Legacy AMD fallback */ - amd_cpuid4(index, &eax, &ebx, &ecx); - } - - if (nb) - *nb =3D amd_init_l3_cache(index); - } else { - /* Intel */ - cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx); - } - if (eax.split.type =3D=3D CTYPE_NULL) return -EIO; =20 @@ -341,6 +312,42 @@ static int cpuid4_cache_lookup_regs(int index, struct = _cpuid4_info_regs *id4, return 0; } =20 +static int amd_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +{ + union _cpuid4_leaf_eax eax; + union _cpuid4_leaf_ebx ebx; + union _cpuid4_leaf_ecx ecx; + u32 ignored; + + if (boot_cpu_has(X86_FEATURE_TOPOEXT) || boot_cpu_data.x86_vendor =3D=3D = X86_VENDOR_HYGON) + cpuid_count(0x8000001d, index, &eax.full, &ebx.full, &ecx.full, &ignored= ); + else + legacy_amd_cpuid4(index, &eax, &ebx, &ecx); + + return cpuid4_info_fill_done(id4, eax, ebx, ecx); +} + +static int intel_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +{ + union _cpuid4_leaf_eax eax; + union _cpuid4_leaf_ebx ebx; + union _cpuid4_leaf_ecx ecx; + u32 ignored; + + cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &ignored); + + return cpuid4_info_fill_done(id4, eax, ebx, ecx); +} + +static int fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +{ + u8 cpu_vendor =3D boot_cpu_data.x86_vendor; + + return (cpu_vendor =3D=3D X86_VENDOR_AMD || cpu_vendor =3D=3D X86_VENDOR_= HYGON) ? + amd_fill_cpuid4_info(index, id4) : + intel_fill_cpuid4_info(index, id4); +} + static int find_num_cache_leaves(struct cpuinfo_x86 *c) { unsigned int eax, ebx, ecx, edx, op; @@ -472,7 +479,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) struct _cpuid4_info_regs id4 =3D {}; int retval; =20 - retval =3D cpuid4_cache_lookup_regs(i, &id4, NULL); + retval =3D intel_fill_cpuid4_info(i, &id4); if (retval < 0) continue; =20 @@ -692,17 +699,23 @@ static void get_cache_id(int cpu, struct _cpuid4_info= _regs *id4) =20 int populate_cache_leaves(unsigned int cpu) { - unsigned int idx, ret; struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci =3D this_cpu_ci->info_list; + u8 cpu_vendor =3D boot_cpu_data.x86_vendor; struct _cpuid4_info_regs id4 =3D {}; - struct amd_northbridge *nb; + struct amd_northbridge *nb =3D NULL; + int idx, ret; =20 for (idx =3D 0; idx < this_cpu_ci->num_leaves; idx++) { - ret =3D cpuid4_cache_lookup_regs(idx, &id4, &nb); + ret =3D fill_cpuid4_info(idx, &id4); if (ret) return ret; + get_cache_id(cpu, &id4); + + if (cpu_vendor =3D=3D X86_VENDOR_AMD || cpu_vendor =3D=3D X86_VENDOR_HYG= ON) + nb =3D amd_init_l3_cache(idx); + ci_info_init(ci++, &id4, nb); __cache_cpumap_setup(cpu, idx, &id4); } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 E3780259CB6 for ; Wed, 19 Mar 2025 12:22:34 +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=1742386956; cv=none; b=PBn6VKH87DjuVAlczkTOq+BMZUBANilALvHqdFVoQVk5NYN6lIhBFGr2CTTveXSuy0tZn0/xhUAURhftr5INcZpdpFLH9QPHnyUVxPmlMt2kKRMwKiq3g/7uFaKg9P6dA/O0yewcui5KuSl2e+3I5LsXSSHaErxGaRV00zQwKvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386956; c=relaxed/simple; bh=th2v8uIZxkNCwoK+iUYJGOuhkFbvACgd0pAZP4DTsZ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aUx4E2tQ0Z7wzgfhqjOPmHld2BtulmS755aTv67G1EIskwmeWXQjat/D7BTqHPrkeyEzhSMhXQ4hnFVEFnIQ/eJwTyKq6Xl1aLMPFEw/PH4NFwuMUla/gv94MXRR3ansaYqxnt6MX/1LAVdB2/odanNWTpnt9azYW2lUGgPMrbY= 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=K26+rURk; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=gqEWumR6; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="K26+rURk"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="gqEWumR6" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ONR2OjC3vdtbfqAVF3kBhl5I8v4wLD4Vyr+ztNwbbYs=; b=K26+rURkI9bF9n+83c/c083Tifu4slrRIIah/pqzolDJePzVLEpCf0tNkj0yWqP/ckh3vz QA1FqHmaC+WG9OOE+CaEPo1VDuZy6aD+zzEBPmsukeTdUtTFGnpcfOADRCL1TLrUbnnHrp zkqQjH13cYcCf5ia+YUSHglF/6d8Nnp+Q4kVEfjxZ+/jqxJH7P6UwbVGyQgVyXj6QPrj5b uArmbA2Nfs4yFUJNK0eLC4aY3bAx18yFIT8IPdQ6/weVBW/eEifMMGaf4imNSBbdl5qPaj QJ+7S0qWjhRU0eXpZA1PZoKHHs1CqWOGXoDiWcCBU8GdNilBdzN4mA3cV6otmw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ONR2OjC3vdtbfqAVF3kBhl5I8v4wLD4Vyr+ztNwbbYs=; b=gqEWumR6b/Ij1QibEC9APb5csNIIObg6SDNZroFzJS74aPecDU+m/2qjjepO5NqVz1Aa1D ItdPdXiy6/dYwYBw== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 16/29] x86/cacheinfo: Rename _cpuid4_info_regs to _cpuid4_info Date: Wed, 19 Mar 2025 13:21:24 +0100 Message-ID: <20250319122137.4004-17-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Parent commits decoupled amd_northbridge from _cpuid4_info_regs, moved AMD L3 northbridge cache_disable_0/1 sysfs code to its own file, and splitted AMD vs. Intel leaf 0x4 handling into: amd_fill_cpuid4_info() intel_fill_cpuid4_info() fill_cpuid4_info() After doing all that, the "_cpuid4_info_regs" name becomes a mouthful. It is also not totally accurate, as the structure holds cpuid4 derived information like cache node ID and size -- not just regs. Rename struct _cpuid4_info_regs to _cpuid4_info. That new name also better matches the AMD/Intel leaf 0x4 functions mentioned above. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 10a79d87ce86..2aaa0f830ec3 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -159,7 +159,7 @@ union _cpuid4_leaf_ecx { u32 full; }; =20 -struct _cpuid4_info_regs { +struct _cpuid4_info { union _cpuid4_leaf_eax eax; union _cpuid4_leaf_ebx ebx; union _cpuid4_leaf_ecx ecx; @@ -295,7 +295,7 @@ static void legacy_amd_cpuid4(int index, union _cpuid4_= leaf_eax *eax, (ebx->split.ways_of_associativity + 1) - 1; } =20 -static int cpuid4_info_fill_done(struct _cpuid4_info_regs *id4, union _cpu= id4_leaf_eax eax, +static int cpuid4_info_fill_done(struct _cpuid4_info *id4, union _cpuid4_l= eaf_eax eax, union _cpuid4_leaf_ebx ebx, union _cpuid4_leaf_ecx ecx) { if (eax.split.type =3D=3D CTYPE_NULL) @@ -312,7 +312,7 @@ static int cpuid4_info_fill_done(struct _cpuid4_info_re= gs *id4, union _cpuid4_le return 0; } =20 -static int amd_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +static int amd_fill_cpuid4_info(int index, struct _cpuid4_info *id4) { union _cpuid4_leaf_eax eax; union _cpuid4_leaf_ebx ebx; @@ -327,7 +327,7 @@ static int amd_fill_cpuid4_info(int index, struct _cpui= d4_info_regs *id4) return cpuid4_info_fill_done(id4, eax, ebx, ecx); } =20 -static int intel_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +static int intel_fill_cpuid4_info(int index, struct _cpuid4_info *id4) { union _cpuid4_leaf_eax eax; union _cpuid4_leaf_ebx ebx; @@ -339,7 +339,7 @@ static int intel_fill_cpuid4_info(int index, struct _cp= uid4_info_regs *id4) return cpuid4_info_fill_done(id4, eax, ebx, ecx); } =20 -static int fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4) +static int fill_cpuid4_info(int index, struct _cpuid4_info *id4) { u8 cpu_vendor =3D boot_cpu_data.x86_vendor; =20 @@ -476,7 +476,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) * parameters cpuid leaf to find the cache details */ for (i =3D 0; i < ci->num_leaves; i++) { - struct _cpuid4_info_regs id4 =3D {}; + struct _cpuid4_info id4 =3D {}; int retval; =20 retval =3D intel_fill_cpuid4_info(i, &id4); @@ -563,7 +563,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) } =20 static int __cache_amd_cpumap_setup(unsigned int cpu, int index, - const struct _cpuid4_info_regs *id4) + const struct _cpuid4_info *id4) { struct cpu_cacheinfo *this_cpu_ci; struct cacheinfo *ci; @@ -620,7 +620,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, } =20 static void __cache_cpumap_setup(unsigned int cpu, int index, - const struct _cpuid4_info_regs *id4) + const struct _cpuid4_info *id4) { struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci, *sibling_ci; @@ -655,7 +655,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int = index, } } =20 -static void ci_info_init(struct cacheinfo *ci, const struct _cpuid4_info_r= egs *id4, +static void ci_info_init(struct cacheinfo *ci, const struct _cpuid4_info *= id4, struct amd_northbridge *nb) { ci->id =3D id4->id; @@ -686,7 +686,7 @@ int init_cache_level(unsigned int cpu) * ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */ -static void get_cache_id(int cpu, struct _cpuid4_info_regs *id4) +static void get_cache_id(int cpu, struct _cpuid4_info *id4) { struct cpuinfo_x86 *c =3D &cpu_data(cpu); unsigned long num_threads_sharing; @@ -702,8 +702,8 @@ int populate_cache_leaves(unsigned int cpu) struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); struct cacheinfo *ci =3D this_cpu_ci->info_list; u8 cpu_vendor =3D boot_cpu_data.x86_vendor; - struct _cpuid4_info_regs id4 =3D {}; struct amd_northbridge *nb =3D NULL; + struct _cpuid4_info id4 =3D {}; int idx, ret; =20 for (idx =3D 0; idx < this_cpu_ci->num_leaves; idx++) { --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 10F3325A2B9 for ; Wed, 19 Mar 2025 12:22:38 +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=1742386960; cv=none; b=lCagF52g716lCgwcHK28Cf5JbgjOkVQ22SIUQENtUP+ZpU5dJheIQUkNYY2KLBj3EZBqdAxs7TbobTU9r5P2VAGJwrguCoy56wgFSzFMNRkOPaq2Owja5uECQzxxN7ubenQEU2fnjN2U4EEqgqgJ/dv86/GrJL+Y8urChi3D06I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386960; c=relaxed/simple; bh=hZDYWSs/QhDo9pcZR+oiMxnjGOUfS/V3f2cive+Ar14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fXRgYDuo2eCNbFiB6I0FBaMEjoW4ER+aRUkGZ0XHLU7lwl3BeHb4Hh5bduJxLhWuk0Q9SY/WUBOTWZ9xb6gUEcPXQJ12xw+JXyCf+MTEhiRavscFHImgWscdD4JY3x+EGT5EYlaYHR7u23sdjriuMG4eIbebyJP4w1ddH7W5nsI= 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=oEcSzosz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2dqoVC9D; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oEcSzosz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2dqoVC9D" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5p0vycv5W2ADV8vRANElH+CDRgTHR6kjEj2QQmS9mbM=; b=oEcSzoszqJ2fQrxuDMwJmRU8NsOt5R9hoJiu19yjm6LFLWtoymkVKZ8Fdwfrq8fEQDBAjq H2+vQC1yWZq34ennryGduzsn95DJZPrioyx4YA9bj47WJOz1lVP0kT3u6dn4Ly8/5F3ugG QhnF26tjEaqm2fGp0hUSaIBpnsyRck2MS/dR/wJymgkjb3yiNrGpSzFuHHUIhHmYsMStA4 zDO2FGlHrG93k2MSGeOGx88A91JcFwMDHj/7jm8b9mqpzMQ74Fv8xbehIK97XihecoPP6E hTCUJttfOaFF7BN8C2QhktI5FyJpm5W8FgxK3LfkZKshWwxCmAp61hxXh40aGA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5p0vycv5W2ADV8vRANElH+CDRgTHR6kjEj2QQmS9mbM=; b=2dqoVC9DXMFbwDugllEP7Z+qCR1TVnKUps3XbfvH+IiSSzsCVKlIRgNTZA8gZ9+YNNxW7R 5TJlmKPTVde5iCBA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 17/29] x86/cacheinfo: Clarify type markers for leaf 0x2 cache descriptors Date: Wed, 19 Mar 2025 13:21:25 +0100 Message-ID: <20250319122137.4004-18-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries. Two separate tables exist for interpreting these descriptors: one for TLBs at intel.c and one for caches at cacheinfo.c. These mapping tables will be merged in further commits, among other improvements to their model. In preparation for this, use more descriptive type names for the leaf 0x2 descriptors associated with cpu caches. Namely: LVL_1_INST =3D> CACHE_L1_INST LVL_1_DATA =3D> CACHE_L1_DATA LVL_2 =3D> CACHE_L2 LVL_3 =3D> CACHE_L3 After the TLB and cache descriptors mapping tables are merged, this will make it clear that such descriptors correspond to cpu caches. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 152 ++++++++++++++++---------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 2aaa0f830ec3..626f55f960dc 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -23,10 +23,10 @@ =20 #include "cpu.h" =20 -#define LVL_1_INST 1 -#define LVL_1_DATA 2 -#define LVL_2 3 -#define LVL_3 4 +#define CACHE_L1_INST 1 +#define CACHE_L1_DATA 2 +#define CACHE_L2 3 +#define CACHE_L3 4 =20 /* Shared last level cache maps */ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map); @@ -52,74 +52,74 @@ struct _cache_table { =20 static const struct _cache_table cache_table[] =3D { - { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */ - { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */ - { 0x09, LVL_1_INST, 32 }, /* 4-way set assoc, 64 byte line size */ - { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */ - { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */ - { 0x0d, LVL_1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */ - { 0x0e, LVL_1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */ - { 0x21, LVL_2, 256 }, /* 8-way set assoc, 64 byte line size */ - { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x23, LVL_3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte = line size */ - { 0x25, LVL_3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte = line size */ - { 0x29, LVL_3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte = line size */ - { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */ - { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */ - { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x3f, LVL_2, 256 }, /* 2-way set assoc, 64 byte line size */ - { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */ - { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */ - { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */ - { 0x44, LVL_2, MB(1) }, /* 4-way set assoc, 32 byte line size */ - { 0x45, LVL_2, MB(2) }, /* 4-way set assoc, 32 byte line size */ - { 0x46, LVL_3, MB(4) }, /* 4-way set assoc, 64 byte line size */ - { 0x47, LVL_3, MB(8) }, /* 8-way set assoc, 64 byte line size */ - { 0x48, LVL_2, MB(3) }, /* 12-way set assoc, 64 byte line size */ - { 0x49, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */ - { 0x4a, LVL_3, MB(6) }, /* 12-way set assoc, 64 byte line size */ - { 0x4b, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */ - { 0x4c, LVL_3, MB(12) }, /* 12-way set assoc, 64 byte line size */ - { 0x4d, LVL_3, MB(16) }, /* 16-way set assoc, 64 byte line size */ - { 0x4e, LVL_2, MB(6) }, /* 24-way set assoc, 64 byte line size */ - { 0x60, LVL_1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte lin= e size */ - { 0x66, LVL_1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ - { 0x67, LVL_1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte lin= e size */ - { 0x68, LVL_1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte lin= e size */ - { 0x78, LVL_2, MB(1) }, /* 4-way set assoc, 64 byte line size */ - { 0x79, LVL_2, 128 }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x7a, LVL_2, 256 }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x7b, LVL_2, 512 }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x7c, LVL_2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte = line size */ - { 0x7d, LVL_2, MB(2) }, /* 8-way set assoc, 64 byte line size */ - { 0x7f, LVL_2, 512 }, /* 2-way set assoc, 64 byte line size */ - { 0x80, LVL_2, 512 }, /* 8-way set assoc, 64 byte line size */ - { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */ - { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */ - { 0x84, LVL_2, MB(1) }, /* 8-way set assoc, 32 byte line size */ - { 0x85, LVL_2, MB(2) }, /* 8-way set assoc, 32 byte line size */ - { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */ - { 0x87, LVL_2, MB(1) }, /* 8-way set assoc, 64 byte line size */ - { 0xd0, LVL_3, 512 }, /* 4-way set assoc, 64 byte line size */ - { 0xd1, LVL_3, MB(1) }, /* 4-way set assoc, 64 byte line size */ - { 0xd2, LVL_3, MB(2) }, /* 4-way set assoc, 64 byte line size */ - { 0xd6, LVL_3, MB(1) }, /* 8-way set assoc, 64 byte line size */ - { 0xd7, LVL_3, MB(2) }, /* 8-way set assoc, 64 byte line size */ - { 0xd8, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */ - { 0xdc, LVL_3, MB(2) }, /* 12-way set assoc, 64 byte line size */ - { 0xdd, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */ - { 0xde, LVL_3, MB(8) }, /* 12-way set assoc, 64 byte line size */ - { 0xe2, LVL_3, MB(2) }, /* 16-way set assoc, 64 byte line size */ - { 0xe3, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */ - { 0xe4, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */ - { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */ - { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */ - { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */ + { 0x06, CACHE_L1_INST, 8 }, /* 4-way set assoc, 32 byte line size */ + { 0x08, CACHE_L1_INST, 16 }, /* 4-way set assoc, 32 byte line size */ + { 0x09, CACHE_L1_INST, 32 }, /* 4-way set assoc, 64 byte line size */ + { 0x0a, CACHE_L1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */ + { 0x0c, CACHE_L1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */ + { 0x0d, CACHE_L1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */ + { 0x0e, CACHE_L1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */ + { 0x21, CACHE_L2, 256 }, /* 8-way set assoc, 64 byte line size */ + { 0x22, CACHE_L3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ + { 0x23, CACHE_L3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ + { 0x25, CACHE_L3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ + { 0x29, CACHE_L3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ + { 0x2c, CACHE_L1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */ + { 0x30, CACHE_L1_INST, 32 }, /* 8-way set assoc, 64 byte line size */ + { 0x39, CACHE_L2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ + { 0x3a, CACHE_L2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line= size */ + { 0x3b, CACHE_L2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line= size */ + { 0x3c, CACHE_L2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ + { 0x3d, CACHE_L2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line= size */ + { 0x3e, CACHE_L2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ + { 0x3f, CACHE_L2, 256 }, /* 2-way set assoc, 64 byte line size */ + { 0x41, CACHE_L2, 128 }, /* 4-way set assoc, 32 byte line size */ + { 0x42, CACHE_L2, 256 }, /* 4-way set assoc, 32 byte line size */ + { 0x43, CACHE_L2, 512 }, /* 4-way set assoc, 32 byte line size */ + { 0x44, CACHE_L2, MB(1) }, /* 4-way set assoc, 32 byte line size */ + { 0x45, CACHE_L2, MB(2) }, /* 4-way set assoc, 32 byte line size */ + { 0x46, CACHE_L3, MB(4) }, /* 4-way set assoc, 64 byte line size */ + { 0x47, CACHE_L3, MB(8) }, /* 8-way set assoc, 64 byte line size */ + { 0x48, CACHE_L2, MB(3) }, /* 12-way set assoc, 64 byte line size */ + { 0x49, CACHE_L3, MB(4) }, /* 16-way set assoc, 64 byte line size */ + { 0x4a, CACHE_L3, MB(6) }, /* 12-way set assoc, 64 byte line size */ + { 0x4b, CACHE_L3, MB(8) }, /* 16-way set assoc, 64 byte line size */ + { 0x4c, CACHE_L3, MB(12) }, /* 12-way set assoc, 64 byte line size */ + { 0x4d, CACHE_L3, MB(16) }, /* 16-way set assoc, 64 byte line size */ + { 0x4e, CACHE_L2, MB(6) }, /* 24-way set assoc, 64 byte line size */ + { 0x60, CACHE_L1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte = line size */ + { 0x66, CACHE_L1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte l= ine size */ + { 0x67, CACHE_L1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte = line size */ + { 0x68, CACHE_L1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte = line size */ + { 0x78, CACHE_L2, MB(1) }, /* 4-way set assoc, 64 byte line size */ + { 0x79, CACHE_L2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ + { 0x7a, CACHE_L2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ + { 0x7b, CACHE_L2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ + { 0x7c, CACHE_L2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ + { 0x7d, CACHE_L2, MB(2) }, /* 8-way set assoc, 64 byte line size */ + { 0x7f, CACHE_L2, 512 }, /* 2-way set assoc, 64 byte line size */ + { 0x80, CACHE_L2, 512 }, /* 8-way set assoc, 64 byte line size */ + { 0x82, CACHE_L2, 256 }, /* 8-way set assoc, 32 byte line size */ + { 0x83, CACHE_L2, 512 }, /* 8-way set assoc, 32 byte line size */ + { 0x84, CACHE_L2, MB(1) }, /* 8-way set assoc, 32 byte line size */ + { 0x85, CACHE_L2, MB(2) }, /* 8-way set assoc, 32 byte line size */ + { 0x86, CACHE_L2, 512 }, /* 4-way set assoc, 64 byte line size */ + { 0x87, CACHE_L2, MB(1) }, /* 8-way set assoc, 64 byte line size */ + { 0xd0, CACHE_L3, 512 }, /* 4-way set assoc, 64 byte line size */ + { 0xd1, CACHE_L3, MB(1) }, /* 4-way set assoc, 64 byte line size */ + { 0xd2, CACHE_L3, MB(2) }, /* 4-way set assoc, 64 byte line size */ + { 0xd6, CACHE_L3, MB(1) }, /* 8-way set assoc, 64 byte line size */ + { 0xd7, CACHE_L3, MB(2) }, /* 8-way set assoc, 64 byte line size */ + { 0xd8, CACHE_L3, MB(4) }, /* 12-way set assoc, 64 byte line size */ + { 0xdc, CACHE_L3, MB(2) }, /* 12-way set assoc, 64 byte line size */ + { 0xdd, CACHE_L3, MB(4) }, /* 12-way set assoc, 64 byte line size */ + { 0xde, CACHE_L3, MB(8) }, /* 12-way set assoc, 64 byte line size */ + { 0xe2, CACHE_L3, MB(2) }, /* 16-way set assoc, 64 byte line size */ + { 0xe3, CACHE_L3, MB(4) }, /* 16-way set assoc, 64 byte line size */ + { 0xe4, CACHE_L3, MB(8) }, /* 16-way set assoc, 64 byte line size */ + { 0xea, CACHE_L3, MB(12) }, /* 24-way set assoc, 64 byte line size */ + { 0xeb, CACHE_L3, MB(18) }, /* 24-way set assoc, 64 byte line size */ + { 0xec, CACHE_L3, MB(24) }, /* 24-way set assoc, 64 byte line size */ }; =20 =20 @@ -521,10 +521,10 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) continue; =20 switch (entry->cache_type) { - case LVL_1_INST: l1i +=3D entry->size; break; - case LVL_1_DATA: l1d +=3D entry->size; break; - case LVL_2: l2 +=3D entry->size; break; - case LVL_3: l3 +=3D entry->size; break; + case CACHE_L1_INST: l1i +=3D entry->size; break; + case CACHE_L1_DATA: l1d +=3D entry->size; break; + case CACHE_L2: l2 +=3D entry->size; break; + case CACHE_L3: l3 +=3D entry->size; break; } } } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 3B69625A2AE for ; Wed, 19 Mar 2025 12:22:40 +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=1742386962; cv=none; b=nu6GkP0us0JrIrKp4jHXlDI95ZiLtokiO+OP1f3xDsdCvKfzIPot+kPtXWEcgnurmF9swD2YNu7jJifWqciEREHyzcUcnQ29sTZ9p+Ac9x/UOjqyS6Tzna0YU72cWGNVyR6uHy2mbVpi6aDAuuc8cAEN5aZHvpJ82sXO54Hw+G8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386962; c=relaxed/simple; bh=+ZO3cstnk+Qtjbiuce3hmo81jqYr5zJP225yAULx8xA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XfPrWePZh93qgdKMri/s6Fn5FAVvVeanTPR4P0k70HizvoNZmAzW4MQMUNIRNQ+SxUvL9cg/+jaRRsB3QXJqgHMtbpgbq/m7H13VWSRRMrGRq3APXPNvDFrPpjt3BxrbXc9UFUBeG82g0TbkmyRnUtHsxGKY1Vz1fUvrxjSgh1s= 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=mqXsJ1Vs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=IKH+z9Qt; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mqXsJ1Vs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="IKH+z9Qt" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386959; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pOrMlzky1UJmtHOQOOS0cJPF/UEaZvfpDRp0DAMYOQk=; b=mqXsJ1Vs1XlQ0KIQvA03e7y73FrIyiRPBm6V4joE+jKWsJ6EP22GfXMG04FRIpG21oT7LN w9Uz/GwJnZiwn5Gue4avUsr/ejrdrP+b6zslsp++KRlXCh+nBrMxxoyj4H0wDd3rveigMY y1WrOnKMrAoHi43lOQ6pvkCKLvguUjCOIB+2v/e4MvnmXHtqKLm7emxprFHqJd13sk2JuJ tRIGDRHP4lv9oECD6Vjm170Keg2rhojdGNuYUaAfaI4ZOS6IFld1uj9UM0bxFJtBwnJz+D EgdAoPbuDRrdEYSbG6gybQ6phwzj3nzrkvRvDK96i5LIBY5FvOHysc4DfkmgqQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386959; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pOrMlzky1UJmtHOQOOS0cJPF/UEaZvfpDRp0DAMYOQk=; b=IKH+z9Qt4xHy1t+/3deJK62U1zzekDiTr9rafqLjrPtim2Ec5K7H3/dAAM/4ymv47Bidgo 1HJ5Djx7fI7zslDA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 18/29] x86/cacheinfo: Use enums for cache descriptor types Date: Wed, 19 Mar 2025 13:21:26 +0100 Message-ID: <20250319122137.4004-19-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The leaf 0x2 one-byte cache descriptor types: CACHE_L1_INST CACHE_L1_DATA CACHE_L2 CACHE_L3 are just discriminators to be used within the cache_table[] mapping. Their specific values are irrelevant. Use enums for such types. Make the enum packed and static assert that its values remain within a single byte so that the cache_table[] array size do not go out of hand. Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid/types.h | 13 +++++++++++++ arch/x86/kernel/cpu/cacheinfo.c | 9 ++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/cpuid/types.h b/arch/x86/include/asm/cpui= d/types.h index 753f6c4514f4..bbbd0bccfce5 100644 --- a/arch/x86/include/asm/cpuid/types.h +++ b/arch/x86/include/asm/cpuid/types.h @@ -2,6 +2,7 @@ #ifndef _ASM_X86_CPUID_TYPES_H #define _ASM_X86_CPUID_TYPES_H =20 +#include #include =20 /* @@ -45,4 +46,16 @@ union leaf_0x2_regs { u8 desc[16]; }; =20 +/* + * Leaf 0x2 1-byte descriptors' cache types + * To be used for their mappings at cache_table[] + */ +enum _cache_table_type { + CACHE_L1_INST, + CACHE_L1_DATA, + CACHE_L2, + CACHE_L3 +} __packed; +static_assert(sizeof(enum _cache_table_type) =3D=3D 1); + #endif /* _ASM_X86_CPUID_TYPES_H */ diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 626f55f960dc..09c5aa9af973 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -23,11 +23,6 @@ =20 #include "cpu.h" =20 -#define CACHE_L1_INST 1 -#define CACHE_L1_DATA 2 -#define CACHE_L2 3 -#define CACHE_L3 4 - /* Shared last level cache maps */ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map); =20 @@ -41,7 +36,7 @@ unsigned int memory_caching_control __ro_after_init; =20 struct _cache_table { u8 descriptor; - char cache_type; + enum _cache_table_type type; short size; }; =20 @@ -520,7 +515,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) if (!entry) continue; =20 - switch (entry->cache_type) { + switch (entry->type) { case CACHE_L1_INST: l1i +=3D entry->size; break; case CACHE_L1_DATA: l1d +=3D entry->size; break; case CACHE_L2: l2 +=3D entry->size; break; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 E820225A327 for ; Wed, 19 Mar 2025 12:22:44 +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=1742386966; cv=none; b=XP1ElUkucvQ/JGeK3KT9KOdgIzOJh4fsJy/dKTTEjlBE0VMzq26f7eYkUKiyiA2tcrQFQCf8gXJkzLnZbQP/r+m5JzR8jz9XpgLee/wmb4bUxXueEq0cr8MqQb4QbxDhET7FJTeIPs5uC3PUoL+sd7HyNz+iy7gDRq0FwQBmKbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386966; c=relaxed/simple; bh=qpujk5hg4CvNJIdMcwYSj/OpC+5m48G6K2q4yWyS4wc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zej731bmB3/JKIiBhGd+WQJ3vs3KIULQxHj98guKptzKFe1FHw+QHLsiFhfJlLQV6RFZXuUxNeDR3HFSiFcap/H8wZVFsa2VCjdnnh7ysUmhwi2sherGIGNGVFKmuXBu1R6NOIIeiLREHfa228TUzWiqB7YBs/Yej3qo2SJ8TP4= 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=w8nNf2u7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kN2WZISo; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="w8nNf2u7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kN2WZISo" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386962; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LzZb8QZvQtSwW+L1cshEDC7N7oXLYfPopw4pUfO7Ik4=; b=w8nNf2u7bHj4s/hvZTQrKXtAgwVThkrHBBmdm1bm5d7yMQHB4CXfvpXGTqLQOQyxWckYTA WXKBbc3m0UHstWgFUdFd7E6EgAE2XC8j/qbnpfufMdoyoTs7To/gOue2p4WBDTcRonz8Gx ZHJoGJjtv4S2La76FayeBfxNk3WWMjXrBzAyQjtnNqRhRATptlTz6IKRq5EaV3CCYbUdgy GwT+Id/RQl/lPASO8i0hNRhXUt4PLoQzUL029FbDIOBWNrAg85Vckws5nwtfLd3yy/o7+1 9VnaHDvCJQn4+sCjEFVCPa/eCvjD9Wc4KrrKzRc4NGvLCL1daxV4J0cVcCiE+w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386962; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LzZb8QZvQtSwW+L1cshEDC7N7oXLYfPopw4pUfO7Ik4=; b=kN2WZISoUs3KkRtuXHxK8tCNw8d+IfQMRjMnDZO/NPnuIQQO+Ga3MYpBiFYbWiaCtOW4Qz s/zZ+10tJjNxZUDA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 19/29] x86/cpu: Use enums for TLB descriptor types Date: Wed, 19 Mar 2025 13:21:27 +0100 Message-ID: <20250319122137.4004-20-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The leaf 0x2 one-byte TLB descriptor types: TLB_INST_4K TLB_INST_4M TLB_INST_2M_4M ... are just discriminators to be used within the intel_tlb_table[] mapping. Their specific values are irrelevant. Use enums for such types. Make the enum packed and static assert that its values remain within a single byte so that the intel_tlb_table[] size do not go out of hand. Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid/types.h | 29 +++++++++++++++++++++++++++++ arch/x86/kernel/cpu/intel.c | 28 +++------------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/arch/x86/include/asm/cpuid/types.h b/arch/x86/include/asm/cpui= d/types.h index bbbd0bccfce5..0c07df192749 100644 --- a/arch/x86/include/asm/cpuid/types.h +++ b/arch/x86/include/asm/cpuid/types.h @@ -58,4 +58,33 @@ enum _cache_table_type { } __packed; static_assert(sizeof(enum _cache_table_type) =3D=3D 1); =20 +/* + * Leaf 0x2 1-byte descriptors' TLB types + * To be used for their mappings at intel_tlb_table[] + * + * Start at 1 since type 0 is reserved for HW byte descriptors which are + * not recognized by the kernel; i.e., those without an explicit mapping. + */ +enum _tlb_table_type { + TLB_INST_4K =3D 1, + TLB_INST_4M, + TLB_INST_2M_4M, + TLB_INST_ALL, + + TLB_DATA_4K, + TLB_DATA_4M, + TLB_DATA_2M_4M, + TLB_DATA_4K_4M, + TLB_DATA_1G, + TLB_DATA_1G_2M_4M, + + TLB_DATA0_4K, + TLB_DATA0_4M, + TLB_DATA0_2M_4M, + + STLB_4K, + STLB_4K_2M, +} __packed; +static_assert(sizeof(enum _tlb_table_type) =3D=3D 1); + #endif /* _ASM_X86_CPUID_TYPES_H */ diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index aeb7d6d48379..def433e0081f 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -626,28 +626,6 @@ static unsigned int intel_size_cache(struct cpuinfo_x8= 6 *c, unsigned int size) } #endif =20 -#define TLB_INST_4K 0x01 -#define TLB_INST_4M 0x02 -#define TLB_INST_2M_4M 0x03 - -#define TLB_INST_ALL 0x05 -#define TLB_INST_1G 0x06 - -#define TLB_DATA_4K 0x11 -#define TLB_DATA_4M 0x12 -#define TLB_DATA_2M_4M 0x13 -#define TLB_DATA_4K_4M 0x14 - -#define TLB_DATA_1G 0x16 -#define TLB_DATA_1G_2M_4M 0x17 - -#define TLB_DATA0_4K 0x21 -#define TLB_DATA0_4M 0x22 -#define TLB_DATA0_2M_4M 0x23 - -#define STLB_4K 0x41 -#define STLB_4K_2M 0x42 - /* * All of leaf 0x2's one-byte TLB descriptors implies the same number of * entries for their respective TLB types. The 0x63 descriptor is an @@ -660,7 +638,7 @@ static unsigned int intel_size_cache(struct cpuinfo_x86= *c, unsigned int size) =20 struct _tlb_table { unsigned char descriptor; - char tlb_type; + enum _tlb_table_type type; unsigned int entries; }; =20 @@ -718,11 +696,11 @@ static void intel_tlb_lookup(const unsigned char desc) intel_tlb_table[k].descriptor !=3D 0; k++) ; =20 - if (intel_tlb_table[k].tlb_type =3D=3D 0) + if (intel_tlb_table[k].type =3D=3D 0) return; =20 entries =3D intel_tlb_table[k].entries; - switch (intel_tlb_table[k].tlb_type) { + switch (intel_tlb_table[k].type) { case STLB_4K: tlb_lli_4k =3D max(tlb_lli_4k, entries); tlb_lld_4k =3D max(tlb_lld_4k, entries); --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 174AA25A344 for ; Wed, 19 Mar 2025 12:22:47 +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=1742386970; cv=none; b=nxVRC7yX7KCrqfQlErGxEYo0gbccSe0XHt644vpknj8aXgopdhQKadKhN0NyHiVUe8hBYsx+5/YBHinYfe16w3Y0DfIB7/9uHEeABbEeqVOgJtYchKXPNdUvXC3+1RJ2SJTKG5yLrJXJxb/ZK2IZPesvFD6+k0JMN8DWZ5UVRFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386970; c=relaxed/simple; bh=CUJe2bYklLnhnhyydCC4EVi4OEmMn3IEP8q6EvvRhQs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EGwdBnQ2NHQFNtJdJiV24B91wC+V36DcCAJrBsSq5lEQe0qrZAEZCkCEgZT4DuOlapVjQRMp+0CsGTDvOPKD37W3vm+RKagUCepvEuS7bPHWFebExwb7Y2xfSsiDpnlv7bVJ4t6D6AGM58g4DxbMAuPmUB7NIFkpwXdymOu/Idk= 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=WIURwz/n; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nv65lBP9; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WIURwz/n"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nv65lBP9" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386966; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kdTZmZD9DRAKBv+i3NGpbcwaos1CetKEKDQ8UM5nj/M=; b=WIURwz/nZSexV/3g3W4iA3BzHJyvGcrQ90dlFdegK7ZH77FYB/4B9BFyc7drGb7shgSUMz jWqx2o098ny/9p2frP2Y9hOXSKEqSmFi0GBnTnD50dSOrgTuUFLy5fjTbXnaXVHhWwB4il ovErREOpDk/IyXv1mo4nUj1gStatFw6WPihBks1T6CLT56RUtHEnr1AH3pD9QO8KwwZcx7 etGYPl2vGZRSLygQYg6bKPZYCROuiKh3r105v3wQy8BImBGFdImX/p9QZvMgT+FSk/QHOS ue1mWXdr2fXUGEH9HoG3R++0fTP3jU2JnEIPg84ZZwXRWgxLqwgIrh+aY+39qA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386966; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kdTZmZD9DRAKBv+i3NGpbcwaos1CetKEKDQ8UM5nj/M=; b=nv65lBP9J7RfsO65ZZgwYHoqmdz4m2Rn9TCrjaUJv1NAkKpxVLscsRGwqH9cKvAgcvwhLg Lz3mov/MkJIlMDBg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 20/29] x86/cpu: Consolidate CPUID leaf 0x2 tables Date: Wed, 19 Mar 2025 13:21:28 +0100 Message-ID: <20250319122137.4004-21-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner CPUID leaf 0x2 describes TLBs and caches. So there are two tables with the respective descriptor constants in intel.c and cacheinfo.c. The tables occupy almost 600 byte and require a loop based lookup for each variant. Combining them into one table occupies exactly 1k rodata and allows to get rid of the loop based lookup by just using the descriptor byte provided by CPUID leaf 0x2 as index into the table, which simplifies the code and reduces text size. The conversion of the intel.c and cacheinfo.c code is done separately. [ darwi: Actually define struct leaf_0x2_table. Tab-align all of cpuid_0x2_table[] mapping entries. Define needed SZ_* macros at instead (merged commit.) Use CACHE_L1_{INST,DATA} as names for L1 cache descriptor types. Set descriptor 0x63 type as TLB_DATA_1G_2M_4M and explain why. Use enums for cache and TLB descriptor types (parent commits.) Start enum types at 1 since type 0 is reserved for unknown descriptors. Ensure that cache and TLB enum type values do not intersect. Add leaf 0x2 table accessor for_each_leaf_0x2_entry() + documentation. ] Signed-off-by: Thomas Gleixner Co-developed-by: Ahmed S. Darwish Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid/leaf_0x2_api.h | 33 +++++- arch/x86/include/asm/cpuid/types.h | 38 +++++-- arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/cpuid_0x2_table.c | 128 ++++++++++++++++++++++ 4 files changed, 192 insertions(+), 9 deletions(-) create mode 100644 arch/x86/kernel/cpu/cpuid_0x2_table.c diff --git a/arch/x86/include/asm/cpuid/leaf_0x2_api.h b/arch/x86/include/a= sm/cpuid/leaf_0x2_api.h index 4c845fc96716..499c6c0707d9 100644 --- a/arch/x86/include/asm/cpuid/leaf_0x2_api.h +++ b/arch/x86/include/asm/cpuid/leaf_0x2_api.h @@ -13,7 +13,8 @@ * invalid 1-byte descriptor returned by the hardware to zero (the NULL * cache/TLB descriptor) before returning it to the caller. * - * Use for_each_leaf_0x2_desc() to iterate over the returned output. + * Use for_each_leaf_0x2_entry() to iterate over the register output in + * parsed form. */ static inline void cpuid_get_leaf_0x2_regs(union leaf_0x2_regs *regs) { @@ -62,4 +63,34 @@ static inline void cpuid_get_leaf_0x2_regs(union leaf_0x= 2_regs *regs) #define for_each_leaf_0x2_desc(regs, desc) \ for (desc =3D &(regs).desc[1]; desc < &(regs).desc[16]; desc++) =20 +/** + * for_each_leaf_0x2_entry() - Iterator for parsed leaf 0x2 descriptors + * @regs: Leaf 0x2 register output, returned by cpuid_get_leaf_0x2_regs() + * @__ptr: u8 pointer, for macro internal use only + * @entry: Pointer to parsed descriptor information at each iteration + * + * Loop over the 1-byte descriptors in the passed leaf 0x2 output registers + * @regs. Provide the parsed information for each descriptor through @ent= ry. + * + * To handle cache-specific descriptors, switch on @entry->c_type. For TLB + * descriptors, switch on @entry->t_type. + * + * Example usage for cache descriptors:: + * + * const struct leaf_0x2_table *entry; + * union leaf_0x2_regs regs; + * u8 *ptr; + * + * cpuid_get_leaf_0x2_regs(®s); + * for_each_leaf_0x2_entry(regs, ptr, entry) { + * switch (entry->c_type) { + * ... + * } + * } + */ +#define for_each_leaf_0x2_entry(regs, __ptr, entry) \ + for (__ptr =3D &(regs).desc[1], entry =3D &cpuid_0x2_table[*__ptr]; \ + __ptr < &(regs).desc[16]; \ + __ptr++, entry =3D &cpuid_0x2_table[*__ptr]) + #endif /* _ASM_X86_CPUID_LEAF_0x2_API_H */ diff --git a/arch/x86/include/asm/cpuid/types.h b/arch/x86/include/asm/cpui= d/types.h index 0c07df192749..4426198f2078 100644 --- a/arch/x86/include/asm/cpuid/types.h +++ b/arch/x86/include/asm/cpuid/types.h @@ -48,25 +48,32 @@ union leaf_0x2_regs { =20 /* * Leaf 0x2 1-byte descriptors' cache types - * To be used for their mappings at cache_table[] + * To be used for their mappings at cpuid_0x2_table[] + * + * Start at 1 since type 0 is reserved for HW byte descriptors which are + * not recognized by the kernel; i.e., those without an explicit mapping. */ enum _cache_table_type { - CACHE_L1_INST, + CACHE_L1_INST =3D 1, CACHE_L1_DATA, CACHE_L2, CACHE_L3 + /* Adjust __TLB_TABLE_TYPE_BEGIN before adding more types */ } __packed; static_assert(sizeof(enum _cache_table_type) =3D=3D 1); =20 +/* + * Ensure that leaf 0x2 cache and TLB type values do not intersect, + * since they share the same type field at struct cpuid_0x2_table. + */ +#define __TLB_TABLE_TYPE_BEGIN (CACHE_L3 + 1) + /* * Leaf 0x2 1-byte descriptors' TLB types - * To be used for their mappings at intel_tlb_table[] - * - * Start at 1 since type 0 is reserved for HW byte descriptors which are - * not recognized by the kernel; i.e., those without an explicit mapping. + * To be used for their mappings at cpuid_0x2_table[] */ enum _tlb_table_type { - TLB_INST_4K =3D 1, + TLB_INST_4K =3D __TLB_TABLE_TYPE_BEGIN, TLB_INST_4M, TLB_INST_2M_4M, TLB_INST_ALL, @@ -87,4 +94,21 @@ enum _tlb_table_type { } __packed; static_assert(sizeof(enum _tlb_table_type) =3D=3D 1); =20 +/* + * Combined parsing table for leaf 0x2 cache and TLB descriptors. + */ + +struct leaf_0x2_table { + union { + enum _cache_table_type c_type; + enum _tlb_table_type t_type; + }; + union { + short c_size; + short entries; + }; +}; + +extern const struct leaf_0x2_table cpuid_0x2_table[256]; + #endif /* _ASM_X86_CPUID_TYPES_H */ diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile index 3a39396d422d..1e26179ff18c 100644 --- a/arch/x86/kernel/cpu/Makefile +++ b/arch/x86/kernel/cpu/Makefile @@ -24,7 +24,7 @@ obj-y +=3D rdrand.o obj-y +=3D match.o obj-y +=3D bugs.o obj-y +=3D aperfmperf.o -obj-y +=3D cpuid-deps.o +obj-y +=3D cpuid-deps.o cpuid_0x2_table.o obj-y +=3D umwait.o obj-y +=3D capflags.o powerflags.o =20 diff --git a/arch/x86/kernel/cpu/cpuid_0x2_table.c b/arch/x86/kernel/cpu/cp= uid_0x2_table.c new file mode 100644 index 000000000000..89bc8db5e9c6 --- /dev/null +++ b/arch/x86/kernel/cpu/cpuid_0x2_table.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#include + +#include "cpu.h" + +#define CACHE_ENTRY(_desc, _type, _size) \ + [_desc] =3D { \ + .c_type =3D (_type), \ + .c_size =3D (_size) / SZ_1K, \ + } + +#define TLB_ENTRY(_desc, _type, _entries) \ + [_desc] =3D { \ + .t_type =3D (_type), \ + .entries =3D (_entries), \ + } + +const struct leaf_0x2_table cpuid_0x2_table[256] =3D { + CACHE_ENTRY(0x06, CACHE_L1_INST, SZ_8K ), /* 4-way set assoc, 32 byte lin= e size */ + CACHE_ENTRY(0x08, CACHE_L1_INST, SZ_16K ), /* 4-way set assoc, 32 byte li= ne size */ + CACHE_ENTRY(0x09, CACHE_L1_INST, SZ_32K ), /* 4-way set assoc, 64 byte li= ne size */ + CACHE_ENTRY(0x0a, CACHE_L1_DATA, SZ_8K ), /* 2 way set assoc, 32 byte lin= e size */ + CACHE_ENTRY(0x0c, CACHE_L1_DATA, SZ_16K ), /* 4-way set assoc, 32 byte li= ne size */ + CACHE_ENTRY(0x0d, CACHE_L1_DATA, SZ_16K ), /* 4-way set assoc, 64 byte li= ne size */ + CACHE_ENTRY(0x0e, CACHE_L1_DATA, SZ_24K ), /* 6-way set assoc, 64 byte li= ne size */ + CACHE_ENTRY(0x21, CACHE_L2, SZ_256K ), /* 8-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x22, CACHE_L3, SZ_512K ), /* 4-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x23, CACHE_L3, SZ_1M ), /* 8-way set assoc, sectored cache,= 64 byte line size */ + CACHE_ENTRY(0x25, CACHE_L3, SZ_2M ), /* 8-way set assoc, sectored cache,= 64 byte line size */ + CACHE_ENTRY(0x29, CACHE_L3, SZ_4M ), /* 8-way set assoc, sectored cache,= 64 byte line size */ + CACHE_ENTRY(0x2c, CACHE_L1_DATA, SZ_32K ), /* 8-way set assoc, 64 byte li= ne size */ + CACHE_ENTRY(0x30, CACHE_L1_INST, SZ_32K ), /* 8-way set assoc, 64 byte li= ne size */ + CACHE_ENTRY(0x39, CACHE_L2, SZ_128K ), /* 4-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3a, CACHE_L2, SZ_192K ), /* 6-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3b, CACHE_L2, SZ_128K ), /* 2-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3c, CACHE_L2, SZ_256K ), /* 4-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3d, CACHE_L2, SZ_384K ), /* 6-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3e, CACHE_L2, SZ_512K ), /* 4-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x3f, CACHE_L2, SZ_256K ), /* 2-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x41, CACHE_L2, SZ_128K ), /* 4-way set assoc, 32 byte line = size */ + CACHE_ENTRY(0x42, CACHE_L2, SZ_256K ), /* 4-way set assoc, 32 byte line = size */ + CACHE_ENTRY(0x43, CACHE_L2, SZ_512K ), /* 4-way set assoc, 32 byte line = size */ + CACHE_ENTRY(0x44, CACHE_L2, SZ_1M ), /* 4-way set assoc, 32 byte line si= ze */ + CACHE_ENTRY(0x45, CACHE_L2, SZ_2M ), /* 4-way set assoc, 32 byte line si= ze */ + CACHE_ENTRY(0x46, CACHE_L3, SZ_4M ), /* 4-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0x47, CACHE_L3, SZ_8M ), /* 8-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0x48, CACHE_L2, SZ_3M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0x49, CACHE_L3, SZ_4M ), /* 16-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0x4a, CACHE_L3, SZ_6M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0x4b, CACHE_L3, SZ_8M ), /* 16-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0x4c, CACHE_L3, SZ_12M ), /* 12-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x4d, CACHE_L3, SZ_16M ), /* 16-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x4e, CACHE_L2, SZ_6M ), /* 24-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0x60, CACHE_L1_DATA, SZ_16K ), /* 8-way set assoc, sectored c= ache, 64 byte line size */ + CACHE_ENTRY(0x66, CACHE_L1_DATA, SZ_8K ), /* 4-way set assoc, sectored ca= che, 64 byte line size */ + CACHE_ENTRY(0x67, CACHE_L1_DATA, SZ_16K ), /* 4-way set assoc, sectored c= ache, 64 byte line size */ + CACHE_ENTRY(0x68, CACHE_L1_DATA, SZ_32K ), /* 4-way set assoc, sectored c= ache, 64 byte line size */ + CACHE_ENTRY(0x78, CACHE_L2, SZ_1M ), /* 4-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0x79, CACHE_L2, SZ_128K ), /* 8-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x7a, CACHE_L2, SZ_256K ), /* 8-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x7b, CACHE_L2, SZ_512K ), /* 8-way set assoc, sectored cach= e, 64 byte line size */ + CACHE_ENTRY(0x7c, CACHE_L2, SZ_1M ), /* 8-way set assoc, sectored cache,= 64 byte line size */ + CACHE_ENTRY(0x7d, CACHE_L2, SZ_2M ), /* 8-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0x7f, CACHE_L2, SZ_512K ), /* 2-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x80, CACHE_L2, SZ_512K ), /* 8-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x82, CACHE_L2, SZ_256K ), /* 8-way set assoc, 32 byte line = size */ + CACHE_ENTRY(0x83, CACHE_L2, SZ_512K ), /* 8-way set assoc, 32 byte line = size */ + CACHE_ENTRY(0x84, CACHE_L2, SZ_1M ), /* 8-way set assoc, 32 byte line si= ze */ + CACHE_ENTRY(0x85, CACHE_L2, SZ_2M ), /* 8-way set assoc, 32 byte line si= ze */ + CACHE_ENTRY(0x86, CACHE_L2, SZ_512K ), /* 4-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0x87, CACHE_L2, SZ_1M ), /* 8-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0xd0, CACHE_L3, SZ_512K ), /* 4-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0xd1, CACHE_L3, SZ_1M ), /* 4-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0xd2, CACHE_L3, SZ_2M ), /* 4-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0xd6, CACHE_L3, SZ_1M ), /* 8-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0xd7, CACHE_L3, SZ_2M ), /* 8-way set assoc, 64 byte line si= ze */ + CACHE_ENTRY(0xd8, CACHE_L3, SZ_4M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xdc, CACHE_L3, SZ_2M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xdd, CACHE_L3, SZ_4M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xde, CACHE_L3, SZ_8M ), /* 12-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xe2, CACHE_L3, SZ_2M ), /* 16-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xe3, CACHE_L3, SZ_4M ), /* 16-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xe4, CACHE_L3, SZ_8M ), /* 16-way set assoc, 64 byte line s= ize */ + CACHE_ENTRY(0xea, CACHE_L3, SZ_12M ), /* 24-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0xeb, CACHE_L3, SZ_18M ), /* 24-way set assoc, 64 byte line = size */ + CACHE_ENTRY(0xec, CACHE_L3, SZ_24M ), /* 24-way set assoc, 64 byte line = size */ + + TLB_ENTRY( 0x01, TLB_INST_4K, 32 ), /* TLB_INST 4 KByte pages, 4-way se= t associative */ + TLB_ENTRY( 0x02, TLB_INST_4M, 2 ), /* TLB_INST 4 MByte pages, full asso= ciative */ + TLB_ENTRY( 0x03, TLB_DATA_4K, 64 ), /* TLB_DATA 4 KByte pages, 4-way se= t associative */ + TLB_ENTRY( 0x04, TLB_DATA_4M, 8 ), /* TLB_DATA 4 MByte pages, 4-way set= associative */ + TLB_ENTRY( 0x05, TLB_DATA_4M, 32 ), /* TLB_DATA 4 MByte pages, 4-way se= t associative */ + TLB_ENTRY( 0x0b, TLB_INST_4M, 4 ), /* TLB_INST 4 MByte pages, 4-way set= associative */ + TLB_ENTRY( 0x4f, TLB_INST_4K, 32 ), /* TLB_INST 4 KByte pages */ + TLB_ENTRY( 0x50, TLB_INST_ALL, 64 ), /* TLB_INST 4 KByte and 2-MByte or= 4-MByte pages */ + TLB_ENTRY( 0x51, TLB_INST_ALL, 128 ), /* TLB_INST 4 KByte and 2-MByte o= r 4-MByte pages */ + TLB_ENTRY( 0x52, TLB_INST_ALL, 256 ), /* TLB_INST 4 KByte and 2-MByte o= r 4-MByte pages */ + TLB_ENTRY( 0x55, TLB_INST_2M_4M, 7 ), /* TLB_INST 2-MByte or 4-MByte pag= es, fully associative */ + TLB_ENTRY( 0x56, TLB_DATA0_4M, 16 ), /* TLB_DATA0 4 MByte pages, 4-way = set associative */ + TLB_ENTRY( 0x57, TLB_DATA0_4K, 16 ), /* TLB_DATA0 4 KByte pages, 4-way = associative */ + TLB_ENTRY( 0x59, TLB_DATA0_4K, 16 ), /* TLB_DATA0 4 KByte pages, fully = associative */ + TLB_ENTRY( 0x5a, TLB_DATA0_2M_4M, 32 ), /* TLB_DATA0 2-MByte or 4 MByte = pages, 4-way set associative */ + TLB_ENTRY( 0x5b, TLB_DATA_4K_4M, 64 ), /* TLB_DATA 4 KByte and 4 MByte p= ages */ + TLB_ENTRY( 0x5c, TLB_DATA_4K_4M, 128 ), /* TLB_DATA 4 KByte and 4 MByte = pages */ + TLB_ENTRY( 0x5d, TLB_DATA_4K_4M, 256 ), /* TLB_DATA 4 KByte and 4 MByte = pages */ + TLB_ENTRY( 0x61, TLB_INST_4K, 48 ), /* TLB_INST 4 KByte pages, full ass= ociative */ + TLB_ENTRY( 0x63, TLB_DATA_1G_2M_4M, 4 ), /* TLB_DATA 1 GByte pages, 4-wa= y set associative + * (plus 32 entries TLB_DATA 2 MByte or 4 MByte pages, not encoded= here) */ + TLB_ENTRY( 0x6b, TLB_DATA_4K, 256 ), /* TLB_DATA 4 KByte pages, 8-way a= ssociative */ + TLB_ENTRY( 0x6c, TLB_DATA_2M_4M, 128 ), /* TLB_DATA 2 MByte or 4 MByte p= ages, 8-way associative */ + TLB_ENTRY( 0x6d, TLB_DATA_1G, 16 ), /* TLB_DATA 1 GByte pages, fully as= sociative */ + TLB_ENTRY( 0x76, TLB_INST_2M_4M, 8 ), /* TLB_INST 2-MByte or 4-MByte pag= es, fully associative */ + TLB_ENTRY( 0xb0, TLB_INST_4K, 128 ), /* TLB_INST 4 KByte pages, 4-way s= et associative */ + TLB_ENTRY( 0xb1, TLB_INST_2M_4M, 4 ), /* TLB_INST 2M pages, 4-way, 8 ent= ries or 4M pages, 4-way entries */ + TLB_ENTRY( 0xb2, TLB_INST_4K, 64 ), /* TLB_INST 4KByte pages, 4-way set= associative */ + TLB_ENTRY( 0xb3, TLB_DATA_4K, 128 ), /* TLB_DATA 4 KByte pages, 4-way s= et associative */ + TLB_ENTRY( 0xb4, TLB_DATA_4K, 256 ), /* TLB_DATA 4 KByte pages, 4-way a= ssociative */ + TLB_ENTRY( 0xb5, TLB_INST_4K, 64 ), /* TLB_INST 4 KByte pages, 8-way se= t associative */ + TLB_ENTRY( 0xb6, TLB_INST_4K, 128 ), /* TLB_INST 4 KByte pages, 8-way s= et associative */ + TLB_ENTRY( 0xba, TLB_DATA_4K, 64 ), /* TLB_DATA 4 KByte pages, 4-way as= sociative */ + TLB_ENTRY( 0xc0, TLB_DATA_4K_4M, 8 ), /* TLB_DATA 4 KByte and 4 MByte pa= ges, 4-way associative */ + TLB_ENTRY( 0xc1, STLB_4K_2M, 1024 ), /* STLB 4 KByte and 2 MByte pages,= 8-way associative */ + TLB_ENTRY( 0xc2, TLB_DATA_2M_4M, 16 ), /* TLB_DATA 2 MByte/4MByte pages,= 4-way associative */ + TLB_ENTRY( 0xca, STLB_4K, 512 ), /* STLB 4 KByte pages, 4-way associati= ve */ +}; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 7ED872571DC for ; Wed, 19 Mar 2025 12:22:50 +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=1742386972; cv=none; b=tI281Xn3OGht/age0DYOgaCvEMulMwAV+qpM1kO9g8TqcpfQj6iN3fqzIta0Mwo13D8Uwh//cMAzPcy8IQm1Pdn5JG1CrSuFGfiKocALkXsMSJVBDCEiwSn6UVPg9eM4eOeZZJ3xoppWrBaGpaTbPciap/Bex5bR2r2RA4CKm30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386972; c=relaxed/simple; bh=CiK1lwxrCBtuvYGueSBv5p1lT2ZJVkfijgnkC9J3wrg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Co4SyuszuyvEbKrZc9iZH4Jr3xwX34huSrMXsKTpj54t43UfrnkhTlqbI5imBoy+ctgugr+FgnSpwrG1APnvDn1hdLKtc3hwn8rJiYFKsDiSos652aRUZ9+surF3ZZm9GL/Xl+BZjPWSMYQKoyCPeCj487HcRDOgbHYk+/XewEg= 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=TC1oPon6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HwQ8FDrQ; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TC1oPon6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HwQ8FDrQ" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wZi49yvNpVLxJkDQKgJXMqCRgaldwHl429IWVD+/wEc=; b=TC1oPon60shRPYcrrHIrQ1U9GmnzuK1VdbNakmeOiR4LkndUnhczzq7KvZOGVu3LvMCHB/ cgrxl7EeDtu7lNauEhJCiWynka2WXvpyGiQt9pgDmB2qanqmaL2HU3hKOGDI84EgU5O/U5 Jbc23b9y+8bAFktaJQ8l5gAsE1zhTKib919aALMJ10hpEjk+Q/F0XQTiHSEihMydczn5jI +ueVAlh+ZloK1zLSMlc7+kXCRQtwRqumvkxnkHJJ7n2uB+y578gGyJPpdbTty3/Kgi5T98 XYshE1xOmkKjVU7/fytDiM4Z4/q353p6PZexsqPrNvbpTwokrFmgvmLg4VAiKg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wZi49yvNpVLxJkDQKgJXMqCRgaldwHl429IWVD+/wEc=; b=HwQ8FDrQTptTsAYCVR2d737dPvCSWK8fIncy4DpNlREZb9TP/pdHh9DjU20IljGBB5AG6U 3ZX3WnnBdpib7KBA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 21/29] x86/cacheinfo: Use consolidated leaf 0x2 descriptor table Date: Wed, 19 Mar 2025 13:21:29 +0100 Message-ID: <20250319122137.4004-22-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries. At previous commits, the mapping tables for such descriptors were merged into one consolidated table. The mapping was also transformed into a hash lookup instead of a loop-based lookup for each descriptor. Use the new consolidated table and its hash-based lookup through the for_each_leaf_0x2_tlb_entry() accessor. Remove the old cache-specific mapping, cache_table[], as it is no longer used. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 114 +++----------------------------- 1 file changed, 8 insertions(+), 106 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 09c5aa9af973..e399bf2a701d 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -34,90 +34,6 @@ static cpumask_var_t cpu_cacheinfo_mask; /* Kernel controls MTRR and/or PAT MSRs. */ unsigned int memory_caching_control __ro_after_init; =20 -struct _cache_table { - u8 descriptor; - enum _cache_table_type type; - short size; -}; - -#define MB(x) ((x) * 1024) - -/* All the cache descriptor types we care about (no TLB or - trace cache entries) */ - -static const struct _cache_table cache_table[] =3D -{ - { 0x06, CACHE_L1_INST, 8 }, /* 4-way set assoc, 32 byte line size */ - { 0x08, CACHE_L1_INST, 16 }, /* 4-way set assoc, 32 byte line size */ - { 0x09, CACHE_L1_INST, 32 }, /* 4-way set assoc, 64 byte line size */ - { 0x0a, CACHE_L1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */ - { 0x0c, CACHE_L1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */ - { 0x0d, CACHE_L1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */ - { 0x0e, CACHE_L1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */ - { 0x21, CACHE_L2, 256 }, /* 8-way set assoc, 64 byte line size */ - { 0x22, CACHE_L3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ - { 0x23, CACHE_L3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x25, CACHE_L3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x29, CACHE_L3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x2c, CACHE_L1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */ - { 0x30, CACHE_L1_INST, 32 }, /* 8-way set assoc, 64 byte line size */ - { 0x39, CACHE_L2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ - { 0x3a, CACHE_L2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line= size */ - { 0x3b, CACHE_L2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line= size */ - { 0x3c, CACHE_L2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ - { 0x3d, CACHE_L2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line= size */ - { 0x3e, CACHE_L2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line= size */ - { 0x3f, CACHE_L2, 256 }, /* 2-way set assoc, 64 byte line size */ - { 0x41, CACHE_L2, 128 }, /* 4-way set assoc, 32 byte line size */ - { 0x42, CACHE_L2, 256 }, /* 4-way set assoc, 32 byte line size */ - { 0x43, CACHE_L2, 512 }, /* 4-way set assoc, 32 byte line size */ - { 0x44, CACHE_L2, MB(1) }, /* 4-way set assoc, 32 byte line size */ - { 0x45, CACHE_L2, MB(2) }, /* 4-way set assoc, 32 byte line size */ - { 0x46, CACHE_L3, MB(4) }, /* 4-way set assoc, 64 byte line size */ - { 0x47, CACHE_L3, MB(8) }, /* 8-way set assoc, 64 byte line size */ - { 0x48, CACHE_L2, MB(3) }, /* 12-way set assoc, 64 byte line size */ - { 0x49, CACHE_L3, MB(4) }, /* 16-way set assoc, 64 byte line size */ - { 0x4a, CACHE_L3, MB(6) }, /* 12-way set assoc, 64 byte line size */ - { 0x4b, CACHE_L3, MB(8) }, /* 16-way set assoc, 64 byte line size */ - { 0x4c, CACHE_L3, MB(12) }, /* 12-way set assoc, 64 byte line size */ - { 0x4d, CACHE_L3, MB(16) }, /* 16-way set assoc, 64 byte line size */ - { 0x4e, CACHE_L2, MB(6) }, /* 24-way set assoc, 64 byte line size */ - { 0x60, CACHE_L1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte = line size */ - { 0x66, CACHE_L1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte l= ine size */ - { 0x67, CACHE_L1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte = line size */ - { 0x68, CACHE_L1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte = line size */ - { 0x78, CACHE_L2, MB(1) }, /* 4-way set assoc, 64 byte line size */ - { 0x79, CACHE_L2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ - { 0x7a, CACHE_L2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ - { 0x7b, CACHE_L2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line= size */ - { 0x7c, CACHE_L2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte li= ne size */ - { 0x7d, CACHE_L2, MB(2) }, /* 8-way set assoc, 64 byte line size */ - { 0x7f, CACHE_L2, 512 }, /* 2-way set assoc, 64 byte line size */ - { 0x80, CACHE_L2, 512 }, /* 8-way set assoc, 64 byte line size */ - { 0x82, CACHE_L2, 256 }, /* 8-way set assoc, 32 byte line size */ - { 0x83, CACHE_L2, 512 }, /* 8-way set assoc, 32 byte line size */ - { 0x84, CACHE_L2, MB(1) }, /* 8-way set assoc, 32 byte line size */ - { 0x85, CACHE_L2, MB(2) }, /* 8-way set assoc, 32 byte line size */ - { 0x86, CACHE_L2, 512 }, /* 4-way set assoc, 64 byte line size */ - { 0x87, CACHE_L2, MB(1) }, /* 8-way set assoc, 64 byte line size */ - { 0xd0, CACHE_L3, 512 }, /* 4-way set assoc, 64 byte line size */ - { 0xd1, CACHE_L3, MB(1) }, /* 4-way set assoc, 64 byte line size */ - { 0xd2, CACHE_L3, MB(2) }, /* 4-way set assoc, 64 byte line size */ - { 0xd6, CACHE_L3, MB(1) }, /* 8-way set assoc, 64 byte line size */ - { 0xd7, CACHE_L3, MB(2) }, /* 8-way set assoc, 64 byte line size */ - { 0xd8, CACHE_L3, MB(4) }, /* 12-way set assoc, 64 byte line size */ - { 0xdc, CACHE_L3, MB(2) }, /* 12-way set assoc, 64 byte line size */ - { 0xdd, CACHE_L3, MB(4) }, /* 12-way set assoc, 64 byte line size */ - { 0xde, CACHE_L3, MB(8) }, /* 12-way set assoc, 64 byte line size */ - { 0xe2, CACHE_L3, MB(2) }, /* 16-way set assoc, 64 byte line size */ - { 0xe3, CACHE_L3, MB(4) }, /* 16-way set assoc, 64 byte line size */ - { 0xe4, CACHE_L3, MB(8) }, /* 16-way set assoc, 64 byte line size */ - { 0xea, CACHE_L3, MB(12) }, /* 24-way set assoc, 64 byte line size */ - { 0xeb, CACHE_L3, MB(18) }, /* 24-way set assoc, 64 byte line size */ - { 0xec, CACHE_L3, MB(24) }, /* 24-way set assoc, 64 byte line size */ -}; - - enum _cache_type { CTYPE_NULL =3D 0, CTYPE_DATA =3D 1, @@ -439,16 +355,6 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =3D find_num_cache_leaves(c); } =20 -static const struct _cache_table *cache_table_get(u8 desc) -{ - for (int i =3D 0; i < ARRAY_SIZE(cache_table); i++) { - if (cache_table[i].descriptor =3D=3D desc) - return &cache_table[i]; - } - - return NULL; -} - void init_intel_cacheinfo(struct cpuinfo_x86 *c) { /* Cache sizes */ @@ -505,21 +411,17 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) =20 /* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level > 1) { - const struct _cache_table *entry; + const struct leaf_0x2_table *entry; union leaf_0x2_regs regs; - u8 *desc; + u8 *ptr; =20 cpuid_get_leaf_0x2_regs(®s); - for_each_leaf_0x2_desc(regs, desc) { - entry =3D cache_table_get(*desc); - if (!entry) - continue; - - switch (entry->type) { - case CACHE_L1_INST: l1i +=3D entry->size; break; - case CACHE_L1_DATA: l1d +=3D entry->size; break; - case CACHE_L2: l2 +=3D entry->size; break; - case CACHE_L3: l3 +=3D entry->size; break; + for_each_leaf_0x2_entry(regs, ptr, entry) { + switch (entry->c_type) { + case CACHE_L1_INST: l1i +=3D entry->c_size; break; + case CACHE_L1_DATA: l1d +=3D entry->c_size; break; + case CACHE_L2: l2 +=3D entry->c_size; break; + case CACHE_L3: l3 +=3D entry->c_size; break; } } } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 7F5AA25A353 for ; Wed, 19 Mar 2025 12:22:53 +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=1742386975; cv=none; b=TtbBmIq7BMXRbCpIxJdPz2sW7yoGh5lDgmrHowiNpxw5pPxGl2lkPDyUzOhKCVZGCF5HQ2WqU8PNGt83dPBBf/8kAN623VLs2CIyvIX9JJwjq1TYcHEUiqoK/sq+acEEFQUwtGFP47NY8T14ZIntRwqqogUxNP8JR6XVdkjcVI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386975; c=relaxed/simple; bh=c7gHvoppGb7F1AXejIsHD9V4Yf6yLhTIOeZOOGfbOYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ERWDg1HTVLw2KXSD8DdLaY6NeXxbB933aKV6ZSnLK5U3aPa3TDZyHoZ7HqojhLn9/1Zv0JUJ3vWIPrBpKTZPhHWhazc3vbZbXBBxk+yKmnB7JMhQgz5w3MZZNm+zDXqs312Js5noiaSmWya6oUge+atZC1zT2cFph9IgEWree0s= 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=Wviva36O; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=iKZ36iDR; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Wviva36O"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="iKZ36iDR" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EgPSlSGzqhyhprXQ1IZvqFI257oe8TfUU7AH4jWJhVE=; b=Wviva36O96fARthoNx4MmIXhw+wC/mUemBnnvb73N63UZbElf7DB0ck+Fm8mM5uUuqrYUT 7xbaxwISXCah/rhGL4inZ9p3xbsrs08amD6lX72NSTwk7CdxYbS3r9zltxOJqcye3HEcOA osSIInZ6jrJS44NknC3H6/SCGeWGenmEcomkX9FtC3qVqbpJYYL6/WuYzuqbI75Z6oO9NM FWGfGhM62CtHlCMLtKcO+QtAkJ5Wf5TCPU1EjxOPIzRYSCg+UJFUCJ0LIJdSuOjOdI7mxX aLkzA91fHoIGlLWH5R0yusbEofa0zSva5tVQE91JoWfasz3llklTRpoVOTaFgQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EgPSlSGzqhyhprXQ1IZvqFI257oe8TfUU7AH4jWJhVE=; b=iKZ36iDRDy4eQJP+DjVHfses+leMJ3tQ/04c3V6jxsubk/2jmT60XxZEhY8A1mJy1txV4q t2a+hEdc859AhTCQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 22/29] x86/cpu: Use consolidated leaf 0x2 descriptor table Date: Wed, 19 Mar 2025 13:21:30 +0100 Message-ID: <20250319122137.4004-23-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries. At previous commits, the mapping tables for such descriptors were merged into one consolidated table. The mapping was also transformed into a hash lookup instead of a loop-based lookup for each descriptor. Use the new consolidated table and its hash-based lookup through the for_each_leaf_0x2_tlb_entry() accessor. Remove the TLB-specific mapping, intel_tlb_table[], as it is now no longer used. Remove the macro, for_each_leaf_0x2_desc(), since the converted code was its last user. Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid/types.h | 10 ++++ arch/x86/kernel/cpu/intel.c | 83 +++--------------------------- 2 files changed, 17 insertions(+), 76 deletions(-) diff --git a/arch/x86/include/asm/cpuid/types.h b/arch/x86/include/asm/cpui= d/types.h index 4426198f2078..10d16f6bbe6f 100644 --- a/arch/x86/include/asm/cpuid/types.h +++ b/arch/x86/include/asm/cpuid/types.h @@ -111,4 +111,14 @@ struct leaf_0x2_table { =20 extern const struct leaf_0x2_table cpuid_0x2_table[256]; =20 +/* + * All of leaf 0x2's one-byte TLB descriptors implies the same number of e= ntries + * for their respective TLB types. TLB descriptor 0x63 is an exception: it + * implies 4 dTLB entries for 1GB pages and 32 dTLB entries for 2MB or 4MB= pages. + * + * Encode that descriptor's dTLB entry count for 2MB/4MB pages here, as th= e entry + * count for dTLB 1GB pages is already encoded at the cpuid_0x2_table[]'s = mapping. + */ +#define TLB_0x63_2M_4M_ENTRIES 32 + #endif /* _ASM_X86_CPUID_TYPES_H */ diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index def433e0081f..e5d814703406 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -626,81 +626,11 @@ static unsigned int intel_size_cache(struct cpuinfo_x= 86 *c, unsigned int size) } #endif =20 -/* - * All of leaf 0x2's one-byte TLB descriptors implies the same number of - * entries for their respective TLB types. The 0x63 descriptor is an - * exception: it implies 4 dTLB entries for 1GB pages 32 dTLB entries - * for 2MB or 4MB pages. Encode descriptor 0x63 dTLB entry count for - * 2MB/4MB pages here, as its count for dTLB 1GB pages is already at the - * intel_tlb_table[] mapping. - */ -#define TLB_0x63_2M_4M_ENTRIES 32 - -struct _tlb_table { - unsigned char descriptor; - enum _tlb_table_type type; - unsigned int entries; -}; - -static const struct _tlb_table intel_tlb_table[] =3D { - { 0x01, TLB_INST_4K, 32}, /* TLB_INST 4 KByte pages, 4-way set associati= ve */ - { 0x02, TLB_INST_4M, 2}, /* TLB_INST 4 MByte pages, full associative */ - { 0x03, TLB_DATA_4K, 64}, /* TLB_DATA 4 KByte pages, 4-way set associati= ve */ - { 0x04, TLB_DATA_4M, 8}, /* TLB_DATA 4 MByte pages, 4-way set associativ= e */ - { 0x05, TLB_DATA_4M, 32}, /* TLB_DATA 4 MByte pages, 4-way set associati= ve */ - { 0x0b, TLB_INST_4M, 4}, /* TLB_INST 4 MByte pages, 4-way set associativ= e */ - { 0x4f, TLB_INST_4K, 32}, /* TLB_INST 4 KByte pages */ - { 0x50, TLB_INST_ALL, 64}, /* TLB_INST 4 KByte and 2-MByte or 4-MByte pa= ges */ - { 0x51, TLB_INST_ALL, 128}, /* TLB_INST 4 KByte and 2-MByte or 4-MByte p= ages */ - { 0x52, TLB_INST_ALL, 256}, /* TLB_INST 4 KByte and 2-MByte or 4-MByte p= ages */ - { 0x55, TLB_INST_2M_4M, 7}, /* TLB_INST 2-MByte or 4-MByte pages, fully = associative */ - { 0x56, TLB_DATA0_4M, 16}, /* TLB_DATA0 4 MByte pages, 4-way set associa= tive */ - { 0x57, TLB_DATA0_4K, 16}, /* TLB_DATA0 4 KByte pages, 4-way associative= */ - { 0x59, TLB_DATA0_4K, 16}, /* TLB_DATA0 4 KByte pages, fully associative= */ - { 0x5a, TLB_DATA0_2M_4M, 32}, /* TLB_DATA0 2-MByte or 4 MByte pages, 4-wa= y set associative */ - { 0x5b, TLB_DATA_4K_4M, 64}, /* TLB_DATA 4 KByte and 4 MByte pages */ - { 0x5c, TLB_DATA_4K_4M, 128}, /* TLB_DATA 4 KByte and 4 MByte pages */ - { 0x5d, TLB_DATA_4K_4M, 256}, /* TLB_DATA 4 KByte and 4 MByte pages */ - { 0x61, TLB_INST_4K, 48}, /* TLB_INST 4 KByte pages, full associative */ - { 0x63, TLB_DATA_1G_2M_4M, 4}, /* TLB_DATA 1 GByte pages, 4-way set assoc= iative - * (plus 32 entries TLB_DATA 2 MByte or 4 MByte pages, not encoded h= ere) */ - { 0x6b, TLB_DATA_4K, 256}, /* TLB_DATA 4 KByte pages, 8-way associative = */ - { 0x6c, TLB_DATA_2M_4M, 128}, /* TLB_DATA 2 MByte or 4 MByte pages, 8-wa= y associative */ - { 0x6d, TLB_DATA_1G, 16}, /* TLB_DATA 1 GByte pages, fully associative */ - { 0x76, TLB_INST_2M_4M, 8}, /* TLB_INST 2-MByte or 4-MByte pages, fully = associative */ - { 0xb0, TLB_INST_4K, 128}, /* TLB_INST 4 KByte pages, 4-way set associat= ive */ - { 0xb1, TLB_INST_2M_4M, 4}, /* TLB_INST 2M pages, 4-way, 8 entries or 4M= pages, 4-way entries */ - { 0xb2, TLB_INST_4K, 64}, /* TLB_INST 4KByte pages, 4-way set associativ= e */ - { 0xb3, TLB_DATA_4K, 128}, /* TLB_DATA 4 KByte pages, 4-way set associat= ive */ - { 0xb4, TLB_DATA_4K, 256}, /* TLB_DATA 4 KByte pages, 4-way associative = */ - { 0xb5, TLB_INST_4K, 64}, /* TLB_INST 4 KByte pages, 8-way set associati= ve */ - { 0xb6, TLB_INST_4K, 128}, /* TLB_INST 4 KByte pages, 8-way set associat= ive */ - { 0xba, TLB_DATA_4K, 64}, /* TLB_DATA 4 KByte pages, 4-way associative */ - { 0xc0, TLB_DATA_4K_4M, 8}, /* TLB_DATA 4 KByte and 4 MByte pages, 4-way= associative */ - { 0xc1, STLB_4K_2M, 1024}, /* STLB 4 KByte and 2 MByte pages, 8-way asso= ciative */ - { 0xc2, TLB_DATA_2M_4M, 16}, /* TLB_DATA 2 MByte/4MByte pages, 4-way ass= ociative */ - { 0xca, STLB_4K, 512}, /* STLB 4 KByte pages, 4-way associative */ - { 0x00, 0, 0 } -}; - -static void intel_tlb_lookup(const unsigned char desc) +static void intel_tlb_lookup(const struct leaf_0x2_table *entry) { - unsigned int entries; - unsigned char k; - - if (desc =3D=3D 0) - return; - - /* look up this descriptor in the table */ - for (k =3D 0; intel_tlb_table[k].descriptor !=3D desc && - intel_tlb_table[k].descriptor !=3D 0; k++) - ; - - if (intel_tlb_table[k].type =3D=3D 0) - return; + short entries =3D entry->entries; =20 - entries =3D intel_tlb_table[k].entries; - switch (intel_tlb_table[k].type) { + switch (entry->t_type) { case STLB_4K: tlb_lli_4k =3D max(tlb_lli_4k, entries); tlb_lld_4k =3D max(tlb_lld_4k, entries); @@ -757,15 +687,16 @@ static void intel_tlb_lookup(const unsigned char desc) =20 static void intel_detect_tlb(struct cpuinfo_x86 *c) { + const struct leaf_0x2_table *entry; union leaf_0x2_regs regs; - u8 *desc; + u8 *ptr; =20 if (c->cpuid_level < 2) return; =20 cpuid_get_leaf_0x2_regs(®s); - for_each_leaf_0x2_desc(regs, desc) - intel_tlb_lookup(*desc); + for_each_leaf_0x2_entry(regs, ptr, entry) + intel_tlb_lookup(entry); } =20 static const struct cpu_dev intel_cpu_dev =3D { --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 8A04725A638 for ; Wed, 19 Mar 2025 12:22:56 +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=1742386978; cv=none; b=SSKw+ZqHLYWowHYOrx5BwEdmvlz5gr1KWq2xCv/BdEBr3idtU2mVA7TjTzNkSE+UMxualKWPcngO+u5wvAPuM6zHeEWzSe4ejQf/i5KyBP3nPBfmBzrlBRWWwCLvuev+34XMLm1WcyNmxAPwLULTO08aU+3vjRU4rrd3Kmvm/MQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386978; c=relaxed/simple; bh=m/K01KgXGVST7kNCinXLCQl3HbIi2grBBdYW7CRD18A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h/2aVkxAnLIL5Bfgg7caUjpETmrLWUGmeLv2p7TTV05BDvYZHJoVjleJaDU2w/gFkQvyQ0lgtsrGlh//0DgDPZ3pl9VihcCi4eq1RYlhEh6+VPTQ8QKdBLliKhuNSY4gJl0J9lYHNAAukPV0L2OqHEiDVz+mq644AxuJJIGrSAA= 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=xRc0N0eE; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=coVm47Q2; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xRc0N0eE"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="coVm47Q2" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386975; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uWsflW12l3h8RYZJxhtdnrG6wdMqcnEWMvzT4KUkGLM=; b=xRc0N0eEIRpip8/GTtJPYV/M0cgNhstBaWnigZcqxLFzqBTHnR00ewoDB48OSd09HKVWFY +nskoPOLOjJ5o0YqPE9vwyd2Vo7xubuQFg9kZLhycfHR7YlsuFj6vW30pSzw8RvQ97E9lp RQD1NIfZEKDgmW8toXYxhDACxfd7oPrPUr5D6YmryZlhJKPQ/4YIBfMCZPiE9pY0PUa7kn lLEXDej9aL2PBIfkRZbmS4lI/szbAl04i0NXpT8zv2CWI0UBTQV01rVXrNniCh7Fhird0S aojy82U+Snmv3tOvDHDGlBjYsGpZIDmSx8COv4UrcsTLN7wVpvXytDhmrIrY7g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386975; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uWsflW12l3h8RYZJxhtdnrG6wdMqcnEWMvzT4KUkGLM=; b=coVm47Q2JACI/rqBLgF8PRT2OjorFTf1kMSm0QzpRVK58Qx1bKYnNtFwchLamRU2yuVo0m Qb8nTRivoAMM5OBg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 23/29] x86/cacheinfo: Separate leaf 0x2 handling and post-processing logic Date: Wed, 19 Mar 2025 13:21:31 +0100 Message-ID: <20250319122137.4004-24-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The logic of init_intel_cacheinfo() is quite convoluted: it mixes leaf 0x4 parsing, leaf 0x2 parsing, plus some post-processing, in a single place. Begin simplifying its logic by extracting the leaf 0x2 parsing code, and the post-processing logic, into their own functions. While at it, rework the SMT LLC topology ID comment for clarity. Suggested-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 106 +++++++++++++++++--------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index e399bf2a701d..b39aad1ecf9c 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -355,14 +355,56 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =3D find_num_cache_leaves(c); } =20 -void init_intel_cacheinfo(struct cpuinfo_x86 *c) +static void intel_cacheinfo_done(struct cpuinfo_x86 *c, unsigned int l3, + unsigned int l2, unsigned int l1i, unsigned int l1d) +{ + /* + * If llc_id is still unset, then cpuid_level < 4, which implies + * that the only possibility left is SMT. Since CPUID(2) doesn't + * specify any shared caches and SMT shares all caches, we can + * unconditionally set LLC ID to the package ID so that all + * threads share it. + */ + if (c->topo.llc_id =3D=3D BAD_APICID) + c->topo.llc_id =3D c->topo.pkg_id; + + c->x86_cache_size =3D l3 ? l3 : (l2 ? l2 : l1i + l1d); + + if (!l2) + cpu_detect_cache_sizes(c); +} + +/* + * Legacy Intel CPUID(2) path if CPUID(4) is not available. + */ +static void intel_cacheinfo_0x2(struct cpuinfo_x86 *c) { - /* Cache sizes */ unsigned int l1i =3D 0, l1d =3D 0, l2 =3D 0, l3 =3D 0; - unsigned int new_l1d =3D 0, new_l1i =3D 0; /* Cache sizes from cpuid(4) */ - unsigned int new_l2 =3D 0, new_l3 =3D 0, i; /* Cache sizes from cpuid(4) = */ - unsigned int l2_id =3D 0, l3_id =3D 0, num_threads_sharing, index_msb; + const struct leaf_0x2_table *entry; + union leaf_0x2_regs regs; + u8 *ptr; + + if (c->cpuid_level < 2) + return; + + cpuid_get_leaf_0x2_regs(®s); + for_each_leaf_0x2_entry(regs, ptr, entry) { + switch (entry->c_type) { + case CACHE_L1_INST: l1i +=3D entry->c_size; break; + case CACHE_L1_DATA: l1d +=3D entry->c_size; break; + case CACHE_L2: l2 +=3D entry->c_size; break; + case CACHE_L3: l3 +=3D entry->c_size; break; + } + } + + intel_cacheinfo_done(c, l3, l2, l1i, l1d); +} + +void init_intel_cacheinfo(struct cpuinfo_x86 *c) +{ struct cpu_cacheinfo *ci =3D get_cpu_cacheinfo(c->cpu_index); + unsigned int l1i =3D 0, l1d =3D 0, l2 =3D 0, l3 =3D 0; + unsigned int l2_id =3D 0, l3_id =3D 0; =20 if (c->cpuid_level > 3) { /* @@ -376,7 +418,8 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) * Whenever possible use cpuid(4), deterministic cache * parameters cpuid leaf to find the cache details */ - for (i =3D 0; i < ci->num_leaves; i++) { + for (int i =3D 0; i < ci->num_leaves; i++) { + unsigned int num_threads_sharing, index_msb; struct _cpuid4_info id4 =3D {}; int retval; =20 @@ -387,18 +430,18 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) switch (id4.eax.split.level) { case 1: if (id4.eax.split.type =3D=3D CTYPE_DATA) - new_l1d =3D id4.size/1024; + l1d =3D id4.size / 1024; else if (id4.eax.split.type =3D=3D CTYPE_INST) - new_l1i =3D id4.size/1024; + l1i =3D id4.size / 1024; break; case 2: - new_l2 =3D id4.size/1024; + l2 =3D id4.size / 1024; num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; index_msb =3D get_count_order(num_threads_sharing); l2_id =3D c->topo.apicid & ~((1 << index_msb) - 1); break; case 3: - new_l3 =3D id4.size/1024; + l3 =3D id4.size / 1024; num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; index_msb =3D get_count_order(num_threads_sharing); l3_id =3D c->topo.apicid & ~((1 << index_msb) - 1); @@ -411,52 +454,19 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) =20 /* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level > 1) { - const struct leaf_0x2_table *entry; - union leaf_0x2_regs regs; - u8 *ptr; - - cpuid_get_leaf_0x2_regs(®s); - for_each_leaf_0x2_entry(regs, ptr, entry) { - switch (entry->c_type) { - case CACHE_L1_INST: l1i +=3D entry->c_size; break; - case CACHE_L1_DATA: l1d +=3D entry->c_size; break; - case CACHE_L2: l2 +=3D entry->c_size; break; - case CACHE_L3: l3 +=3D entry->c_size; break; - } - } + intel_cacheinfo_0x2(c); + return; } =20 - if (new_l1d) - l1d =3D new_l1d; - - if (new_l1i) - l1i =3D new_l1i; - - if (new_l2) { - l2 =3D new_l2; + if (l2) { c->topo.llc_id =3D l2_id; c->topo.l2c_id =3D l2_id; } =20 - if (new_l3) { - l3 =3D new_l3; + if (l3) c->topo.llc_id =3D l3_id; - } =20 - /* - * If llc_id is not yet set, this means cpuid_level < 4 which in - * turns means that the only possibility is SMT (as indicated in - * cpuid1). Since cpuid2 doesn't specify shared caches, and we know - * that SMT shares all caches, we can unconditionally set cpu_llc_id to - * c->topo.pkg_id. - */ - if (c->topo.llc_id =3D=3D BAD_APICID) - c->topo.llc_id =3D c->topo.pkg_id; - - c->x86_cache_size =3D l3 ? l3 : (l2 ? l2 : (l1i+l1d)); - - if (!l2) - cpu_detect_cache_sizes(c); + intel_cacheinfo_done(c, l3, l2, l1i, l1d); } =20 static int __cache_amd_cpumap_setup(unsigned int cpu, int index, --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 D790B25B686 for ; Wed, 19 Mar 2025 12:22:59 +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=1742386981; cv=none; b=Dse0eRVSTn2EwzjEAH0GW6mM3sQrGQvL8v8WMwIq+amLrKSVoQm5xYvy12LNRwTAWTe2T//2D8VNHW/jHs8I1Z73/zRMUGWYyrqSReNdOU47B4OjktwjTDQ0E7d1SrDh81ozf/71UFNVbScdPm614BV2cIJJbHdr+UWWYetEjuw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386981; c=relaxed/simple; bh=bgH4GnCFa3f6HaDcCjTmhIWuxb5iDGJ5lLVgqaR1N8Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GEqK6Hh7hIf5fopBinE3EfqVnayAZcprGOAXmp5MPTHfZqt9UdflwNd9Z6M1W0kESLgOv3/h4+zYKxLPW+sRUQUC7xaxyp7wg9yMwFi7EduwRQN7nJ8B3BxEJV0bnaUPVeAWI7lejLU+fsGgn5CxfxIC6Qm6ubX7w8Z/IDjcl+c= 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=CrPa8YUo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cYCPtCBw; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CrPa8YUo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cYCPtCBw" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2r8iJX+a/1PDmMGUecVZ46+84pOafUoXB4WDPGnjw4A=; b=CrPa8YUoXyGd8drG4fLOr6INLHeUAWMBZuHnmFztXPaQZG6h/E5ORQyj1m8Mo/0Vg1NRPY 7kgRb/RvWFcUhKexQQjLy7n7Vn9eMF24jn5epXTijUEIGkaSqIlfEaMSVopJ6uqvKBJolW o72L7kuVx/Z8rLJF3otF+rdTV9QNvcNfwzSgMeBYtuaTSjJb2TI3IMVEfJRkQlEAFoe5EJ s70yn6x51IftU/10d6R3or54GGwXj7tlcIihHnEXW1y27ZYr/9Sk670LhVFZ7FRqlf1LY/ MVpLpYbAqgR4vzFnu1JJV0J3Xio1kLQmGqPhlHknH0V3Zc7/OqBCDjq17bEbYg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2r8iJX+a/1PDmMGUecVZ46+84pOafUoXB4WDPGnjw4A=; b=cYCPtCBwBO1CrepG8qblhiU4pccPbQT1HsErS9H5Iyijv4DXRH1/oGyxsU8ClRktva2Une nNhVkgv9FMyKaBBA== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 24/29] x86/cacheinfo: Separate Intel leaf 0x4 handling Date: Wed, 19 Mar 2025 13:21:32 +0100 Message-ID: <20250319122137.4004-25-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" init_intel_cacheinfo() was overly complex. It parsed leaf 0x4 data, leaf 0x2 data, and performed post-processing, all within one function. Parent commit moved leaf 0x2 parsing and the post-processing logic into their own functions. Continue the refactoring by extracting leaf 0x4 parsing into its own function. Initialize local L2/L3 topology ID variables to BAD_APICID by default, thus ensuring they can be used unconditionally. Suggested-by: Thomas Gleixner Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 110 ++++++++++++++++---------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index b39aad1ecf9c..72cc32d22c4d 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -400,73 +400,71 @@ static void intel_cacheinfo_0x2(struct cpuinfo_x86 *c) intel_cacheinfo_done(c, l3, l2, l1i, l1d); } =20 -void init_intel_cacheinfo(struct cpuinfo_x86 *c) +static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c) { struct cpu_cacheinfo *ci =3D get_cpu_cacheinfo(c->cpu_index); - unsigned int l1i =3D 0, l1d =3D 0, l2 =3D 0, l3 =3D 0; - unsigned int l2_id =3D 0, l3_id =3D 0; - - if (c->cpuid_level > 3) { - /* - * There should be at least one leaf. A non-zero value means - * that the number of leaves has been initialized. - */ - if (!ci->num_leaves) - ci->num_leaves =3D find_num_cache_leaves(c); + unsigned int l2_id =3D BAD_APICID, l3_id =3D BAD_APICID; + unsigned int l1d =3D 0, l1i =3D 0, l2 =3D 0, l3 =3D 0; =20 - /* - * Whenever possible use cpuid(4), deterministic cache - * parameters cpuid leaf to find the cache details - */ - for (int i =3D 0; i < ci->num_leaves; i++) { - unsigned int num_threads_sharing, index_msb; - struct _cpuid4_info id4 =3D {}; - int retval; + if (c->cpuid_level < 4) + return false; =20 - retval =3D intel_fill_cpuid4_info(i, &id4); - if (retval < 0) - continue; + /* + * There should be at least one leaf. A non-zero value means + * that the number of leaves has been previously initialized. + */ + if (!ci->num_leaves) + ci->num_leaves =3D find_num_cache_leaves(c); =20 - switch (id4.eax.split.level) { - case 1: - if (id4.eax.split.type =3D=3D CTYPE_DATA) - l1d =3D id4.size / 1024; - else if (id4.eax.split.type =3D=3D CTYPE_INST) - l1i =3D id4.size / 1024; - break; - case 2: - l2 =3D id4.size / 1024; - num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; - index_msb =3D get_count_order(num_threads_sharing); - l2_id =3D c->topo.apicid & ~((1 << index_msb) - 1); - break; - case 3: - l3 =3D id4.size / 1024; - num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; - index_msb =3D get_count_order(num_threads_sharing); - l3_id =3D c->topo.apicid & ~((1 << index_msb) - 1); - break; - default: - break; - } + if (!ci->num_leaves) + return false; + + for (int i =3D 0; i < ci->num_leaves; i++) { + unsigned int num_threads_sharing, index_msb; + struct _cpuid4_info id4 =3D {}; + int ret; + + ret =3D intel_fill_cpuid4_info(i, &id4); + if (ret < 0) + continue; + + switch (id4.eax.split.level) { + case 1: + if (id4.eax.split.type =3D=3D CTYPE_DATA) + l1d =3D id4.size / 1024; + else if (id4.eax.split.type =3D=3D CTYPE_INST) + l1i =3D id4.size / 1024; + break; + case 2: + l2 =3D id4.size / 1024; + num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; + index_msb =3D get_count_order(num_threads_sharing); + l2_id =3D c->topo.apicid & ~((1 << index_msb) - 1); + break; + case 3: + l3 =3D id4.size / 1024; + num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; + index_msb =3D get_count_order(num_threads_sharing); + l3_id =3D c->topo.apicid & ~((1 << index_msb) - 1); + break; + default: + break; } } =20 + c->topo.l2c_id =3D l2_id; + c->topo.llc_id =3D (l3_id =3D=3D BAD_APICID) ? l2_id : l3_id; + intel_cacheinfo_done(c, l3, l2, l1i, l1d); + return true; +} + +void init_intel_cacheinfo(struct cpuinfo_x86 *c) +{ /* Don't use CPUID(2) if CPUID(4) is supported. */ - if (!ci->num_leaves && c->cpuid_level > 1) { - intel_cacheinfo_0x2(c); + if (intel_cacheinfo_0x4(c)) return; - } - - if (l2) { - c->topo.llc_id =3D l2_id; - c->topo.l2c_id =3D l2_id; - } - - if (l3) - c->topo.llc_id =3D l3_id; =20 - intel_cacheinfo_done(c, l3, l2, l1i, l1d); + intel_cacheinfo_0x2(c); } =20 static int __cache_amd_cpumap_setup(unsigned int cpu, int index, --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 817A625A638 for ; Wed, 19 Mar 2025 12:23:02 +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=1742386984; cv=none; b=J+ZVc4AWk8Jc6vMXExhxAOpw+LfIjA5BTR80t/My1vy6yR8fOJYXWQmyc8oojgFosUbrBDmfIw8BpnTLYWXrE3a72kQcKpNx9pVdPcEvGk6p2wnOa6OZ4KWWd3e4sLFV2WJcqyW7I5aksv/x2Xvt3qgvqz5gPnyXii67lQlmP0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386984; c=relaxed/simple; bh=3BKY/tdXYfNHltAGaSPaLrYi3N/AfCY072jY5D3JS/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JPhxameBszyS/B7YpUYGA9vGgrlDkA+R8dK1AAq+fd87BI3d1Pz7BieVbHAicy6njOQPC/C/D7ZsJ0/ob2scp6Jm/ixWjhMlyfNjD6Wncb/HVHMiZmFHKUWJ3Bq0jbaVA/WeKwkDxasqyjym/CKh2GDcekfNRChrK9rmlsq2/Ns= 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=y1wely5e; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=p7HNOVck; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="y1wely5e"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="p7HNOVck" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a7qWyKmI7d4ylHBghOpPKLLOgaxJRqfMrX5QN8Pw+nA=; b=y1wely5eXTuvDzFYV3+n/Qxf+tydqtrN5ZM69Tqlj/N8wiE14OqgLzNxS/PWE9EajZmvsN a7dcRStACe59XNtl1Kqt6OkbbVZ1bveqXWdvI4pGIKugYuvxyHxkCRNaLQRPHQjy7ubRkQ FJahJT2DkvijpmIya4ve8kaNODGHfiE1ETx/biVLkjlSbyY7dtySO02K9XmolxnfNEPbAI hNVJ7Kd2DxpPHba1RPspI6VBa/AzSfOBeTKwvH7h2az2dkR6mbWcVcWkH2PSLqljcNwzTz Fph5zRB1UVj7jZ/On0vT4IV3BhdmnmkD+zM/lxEQwTId2PVwo8n9+BKqs+hp6w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a7qWyKmI7d4ylHBghOpPKLLOgaxJRqfMrX5QN8Pw+nA=; b=p7HNOVckIJborKEP+40ZWMEqjcQbYWH3r2ekSJxUN0KX5p2bT2BglxDcU3dPjfe6c6HtDo shraCvnt+lzlrvCQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 25/29] x86/cacheinfo: Extract out cache level topology ID calculation Date: Wed, 19 Mar 2025 13:21:33 +0100 Message-ID: <20250319122137.4004-26-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For Intel leaf 0x4 parsing, refactor the cache level topology ID calculation code into its own method instead of repeating the same logic twice for L2 and L3. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 72cc32d22c4d..7b274da7c725 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -400,6 +400,16 @@ static void intel_cacheinfo_0x2(struct cpuinfo_x86 *c) intel_cacheinfo_done(c, l3, l2, l1i, l1d); } =20 +static unsigned int calc_cache_topo_id(struct cpuinfo_x86 *c, const struct= _cpuid4_info *id4) +{ + unsigned int num_threads_sharing; + int index_msb; + + num_threads_sharing =3D 1 + id4->eax.split.num_threads_sharing; + index_msb =3D get_count_order(num_threads_sharing); + return c->topo.apicid & ~((1 << index_msb) - 1); +} + static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c) { struct cpu_cacheinfo *ci =3D get_cpu_cacheinfo(c->cpu_index); @@ -420,7 +430,6 @@ static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c) return false; =20 for (int i =3D 0; i < ci->num_leaves; i++) { - unsigned int num_threads_sharing, index_msb; struct _cpuid4_info id4 =3D {}; int ret; =20 @@ -437,15 +446,11 @@ static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c) break; case 2: l2 =3D id4.size / 1024; - num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; - index_msb =3D get_count_order(num_threads_sharing); - l2_id =3D c->topo.apicid & ~((1 << index_msb) - 1); + l2_id =3D calc_cache_topo_id(c, &id4); break; case 3: l3 =3D id4.size / 1024; - num_threads_sharing =3D 1 + id4.eax.split.num_threads_sharing; - index_msb =3D get_count_order(num_threads_sharing); - l3_id =3D c->topo.apicid & ~((1 << index_msb) - 1); + l3_id =3D calc_cache_topo_id(c, &id4); break; default: break; --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 915D725BAD1 for ; Wed, 19 Mar 2025 12:23:05 +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=1742386987; cv=none; b=iQYgko7386i+DQAsc9LI//uqwqZv9JStAta5j/hz3N7nRMUursTKIX91RB0atCz11/+bt8C28myCCW60/8qD/zFSQJx80NghHE/xc868aTp1AKp+bBgrN/16nD3z1CXhN6urBmRrlqEhdqC2DpP/YvypuHyTVkaFqTj340/4kyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386987; c=relaxed/simple; bh=dtUkhO8PLZy8aCCLf9saP9TM0LaUafhrLDYgee7UAWM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hxAd6VvoFpsYc8wVgqeg4pIFRzeK7dK9M7ag2+U7c3HYzGny9HeubkclCbop1kZqJPm6jQjJvPRmN9W2fgOIaET4uKgj0UzYqRME21PynhrUGKiUAew61xnuEyG8eMHq9xDB+3RZ5Iq8k0cQORir8cSutx2Y3AlP3ac1hKHRvZw= 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=1eh51jMd; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mD0JCSxG; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1eh51jMd"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mD0JCSxG" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386984; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PUWiDF6/fj+c8JQhlkPB6lM1axYjhCyaaIXJQ8l4Ag8=; b=1eh51jMdXabfnpbHMylnxC+28vS0hukFOe101zN33sN6Uc2Q8MNLZs2lDU5+BuZWifRtO/ L1tTOTEcJE6qAL4AXcoANa2S5jsJjcCOXtKd98oUx/mFkn5C2AHsdyIssed6mW/8+DgNWu k9dAAGqVDYUkSv+p15mtTxPeuKbMzIeoZSyXTYsQ4MOc85Ia0MMj+mEk7g6bIpF5CFNyrG uHdbzpM07YNx7wN43oyiUtWV0pYkyRJGuABj4nofb3F08/5Lwg79ayQrKZaky992LWhU5E jngQ/wizQA8yFfJEmR0wjL0eRzOySAarToJCOXItzaGbFvREa7DieWCqrPbV9g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386984; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PUWiDF6/fj+c8JQhlkPB6lM1axYjhCyaaIXJQ8l4Ag8=; b=mD0JCSxGWL7ycLhIAgelfuydjSQwro20Z4YKK1JH9A/QnudszZZRzPKii1Zwrg2oXMJlVO 6HvwUipn1N8KVRCQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 26/29] x86/cacheinfo: Extract out cache self-snoop checks Date: Wed, 19 Mar 2025 13:21:34 +0100 Message-ID: <20250319122137.4004-27-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The logic of not doing a cache flush if the CPU declares cache self snooping support is repeated across the x86/cacheinfo code. Extract it into its own function. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 7b274da7c725..231470cdc1da 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -646,6 +646,17 @@ int populate_cache_leaves(unsigned int cpu) static unsigned long saved_cr4; static DEFINE_RAW_SPINLOCK(cache_disable_lock); =20 +/* + * Cache flushing is the most time-consuming step when programming the + * MTRRs. On many Intel CPUs without known erratas, it can be skipped + * if the CPU declares cache self-snooping support. + */ +static void maybe_flush_caches(void) +{ + if (!static_cpu_has(X86_FEATURE_SELFSNOOP)) + wbinvd(); +} + void cache_disable(void) __acquires(cache_disable_lock) { unsigned long cr0; @@ -663,14 +674,7 @@ void cache_disable(void) __acquires(cache_disable_lock) cr0 =3D read_cr0() | X86_CR0_CD; write_cr0(cr0); =20 - /* - * Cache flushing is the most time-consuming step when programming - * the MTRRs. Fortunately, as per the Intel Software Development - * Manual, we can skip it if the processor supports cache self- - * snooping. - */ - if (!static_cpu_has(X86_FEATURE_SELFSNOOP)) - wbinvd(); + maybe_flush_caches(); =20 /* Save value of CR4 and clear Page Global Enable (bit 7) */ if (cpu_feature_enabled(X86_FEATURE_PGE)) { @@ -685,9 +689,7 @@ void cache_disable(void) __acquires(cache_disable_lock) if (cpu_feature_enabled(X86_FEATURE_MTRR)) mtrr_disable(); =20 - /* Again, only flush caches if we have to. */ - if (!static_cpu_has(X86_FEATURE_SELFSNOOP)) - wbinvd(); + maybe_flush_caches(); } =20 void cache_enable(void) __releases(cache_disable_lock) --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 6BDA3257450 for ; Wed, 19 Mar 2025 12:23:08 +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=1742386989; cv=none; b=m5716yof04E6y0giU0e6E8/QXn2akz5sRfIZLBHI8NQGE+0TNgZVdDrmtrJCXcRI0Hi9L3WInl1WkgPqORZBtyOeCk3+7eIOAxluzvT8u7URfVyWuDDzf0i2jVhHtWUs/jXAfaynctduh+y2PEROhrnGUOgc7sxS0RRPIN/usf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386989; c=relaxed/simple; bh=GnEaEdfuuR0FliRM85tZyDwxGyhP11E1LMSSWP4eZIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fxvAAuDXX4WCpe7CP/nioGbu4OuJBXNwYKNnsLoUcB3sNga3cvoZjTE4lKczA1eeyzgzom2wi/2AZbxZwesHJyaF/QlMYCAzuJ+Qoyj4d8/szNYL162i2oOD4ru4948uTVPD7n0mcjDTsXOwaRkiRgPw9J1XZPLGtAmwCBncxpM= 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=BPe3LM2U; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3lgbyE+J; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BPe3LM2U"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3lgbyE+J" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DSMxaAJaYwtGvvqw/1WWGUWS0pnZ3kH5ba5j797FF34=; b=BPe3LM2U47WpTpKv/fnu4rtvAcTQsCt2GUuf4uiJwSIIXx691X2HHswhrtKsWVXSQ5lTl7 Q+MT5T0kY/5/9lBzEA02C1wdQw/2XeNHjiRFKUpUpQBk2GDt9AV38Rwq58ELSGgpBGRFE3 KXc7SzSV1ndRrMPzwsB//IoVxe03vEMAcuC+PUl6f0uPTdd/VUtZiEOr4FX1zcDTov9f0K bIg6qN7Z7Y7W1crQphzHMUpfpIJtgCyQwpCDR21xCJFW8meJCtI8qDPEW9HYfDtMj2elc6 VVySW16gSPJIwTZvU9VkH5ZPwWT5puhWbdsV0rfsOU2HopArc3v8pKQ8XhG22g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DSMxaAJaYwtGvvqw/1WWGUWS0pnZ3kH5ba5j797FF34=; b=3lgbyE+Jn5Defenm1c6dBzLzZH8kQBiaaTxuSwdjXo4dIkqFiH/hKqMT5i1V60NZwcAxc0 AjMX/UeYtoAkjTBQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 27/29] x86/cacheinfo: Relocate leaf 0x4 cache_type mapping Date: Wed, 19 Mar 2025 13:21:35 +0100 Message-ID: <20250319122137.4004-28-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The cache_type_map[] array is used to map Intel leaf 0x4 cache_type values to their corresponding types at . Move that array's definition after the actual CPUID leaf 0x4 structures, instead of having it in the middle of AMD leaf 0x4 emulation code. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 231470cdc1da..e0d531e27ff3 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -78,6 +78,14 @@ struct _cpuid4_info { unsigned long size; }; =20 +/* Map CPUID(4) EAX.cache_type to linux/cacheinfo.h types */ +static const enum cache_type cache_type_map[] =3D { + [CTYPE_NULL] =3D CACHE_TYPE_NOCACHE, + [CTYPE_DATA] =3D CACHE_TYPE_DATA, + [CTYPE_INST] =3D CACHE_TYPE_INST, + [CTYPE_UNIFIED] =3D CACHE_TYPE_UNIFIED, +}; + /* * Fallback AMD CPUID(4) emulation * AMD CPUs with TOPOEXT can just use CPUID(0x8000001d) @@ -131,13 +139,6 @@ static const unsigned short assocs[] =3D { static const unsigned char levels[] =3D { 1, 1, 2, 3 }; static const unsigned char types[] =3D { 1, 2, 3, 3 }; =20 -static const enum cache_type cache_type_map[] =3D { - [CTYPE_NULL] =3D CACHE_TYPE_NOCACHE, - [CTYPE_DATA] =3D CACHE_TYPE_DATA, - [CTYPE_INST] =3D CACHE_TYPE_INST, - [CTYPE_UNIFIED] =3D CACHE_TYPE_UNIFIED, -}; - static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx) { --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 60823257ACA for ; Wed, 19 Mar 2025 12:23:11 +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=1742386993; cv=none; b=JyTaq8Fr5kbN2NS5UvbSwAfAryQblqlctuhizyzpswfT65KxlEB3QcL/tmfXQzbSIYEzCbZC4MeasnuJjINDF6N1ZPTYns4hhcmsURcehuf0sgzAMIDBZyY9uGI+zxfl++/FK1QNnpCk4L63PUmeNtcSo0dgbM/7qdWrGg58jis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386993; c=relaxed/simple; bh=PlewxVsaajbPLBCzxPgpr+F21pWwMFCKmJi3cieID4k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dUAgbNRVa4lRLoIzqb/VaNMF+xXT6qemOfTP0/2ZbNSlVQzKQfDVHPvjc6LItZhNTydqwR566PQIh8gi08SgSlGfn5xQX0JADNlPeBUNq5SCbXS0Fi+JxVcuWBvK9SbryDLnGC+qMa4PRB0yf4S6PGLWwQvxnvhMBHGpiEi4Ue4= 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=xorIDDWs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kYkAoXFP; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xorIDDWs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kYkAoXFP" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386990; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2FugV0TUKiWHscZanPSB2dnEm4OQMHiUSBRa/nA1w/I=; b=xorIDDWsf8pPvCc+AtLd88BsSZ5IzsSzEPs8L1MMuaAGewjP1yZLzEreYJfHmk8pvx6lFq vHtNXTiSjKMz17sfER9lPaKjmrygm1+0O/WfvxktVtEST33UwB3i8m0FU9fQEwANtFYwoz G4a0s7Qi3wH9kNceQe5aWbnTZuIXG+SmVicEOcM+f6vfuZlYqEMdYBp976M0RaBDdAZxuK KVNJFqowvMgKFX9a8BHV/7ZHf0ot0Jq7l68bY1O979O9GicDjlFquLRhX6upNZVhomO76d 2KtIFbLXCTjvuCNYetK4KNn4pVA5R5kSWLzHFgjIcisJS6+NBkKS5y0W+PNP2Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386990; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2FugV0TUKiWHscZanPSB2dnEm4OQMHiUSBRa/nA1w/I=; b=kYkAoXFPtUdrYqPA7YvoX086XBbtM90qezZrWv6yGAHQYEiAHerkg5CabIgrl0H0VAFOWm /L8KoH5VLyXDPrBQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 28/29] x86/cacheinfo: Introduce cpuid_amd_hygon_has_l3_cache() Date: Wed, 19 Mar 2025 13:21:36 +0100 Message-ID: <20250319122137.4004-29-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Multiple code paths at cacheinfo.c and amd_nb.c check for AMD/Hygon CPUs L3 cache presensce by directly checking leaf 0x80000006 EDX output. Extract that logic into its own function. While at it, rework the AMD/Hygon LLC topology ID caclculation comments for clarity. Signed-off-by: Ahmed S. Darwish --- arch/x86/include/asm/cpuid/api.h | 9 +++++++++ arch/x86/kernel/amd_nb.c | 7 +++---- arch/x86/kernel/cpu/cacheinfo.c | 32 ++++++++++++++------------------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/arch/x86/include/asm/cpuid/api.h b/arch/x86/include/asm/cpuid/= api.h index 9c180c9cc58e..bf75c6267311 100644 --- a/arch/x86/include/asm/cpuid/api.h +++ b/arch/x86/include/asm/cpuid/api.h @@ -207,4 +207,13 @@ static inline u32 hypervisor_cpuid_base(const char *si= g, u32 leaves) return 0; } =20 +/* + * CPUID(0x80000006) parsing helpers + */ + +static inline bool cpuid_amd_hygon_has_l3_cache(void) +{ + return cpuid_edx(0x80000006); +} + #endif /* _ASM_X86_CPUID_API_H */ diff --git a/arch/x86/kernel/amd_nb.c b/arch/x86/kernel/amd_nb.c index 24d7a87edf9c..49af8fd68d88 100644 --- a/arch/x86/kernel/amd_nb.c +++ b/arch/x86/kernel/amd_nb.c @@ -13,7 +13,9 @@ #include #include #include + #include +#include =20 static u32 *flush_words; =20 @@ -91,10 +93,7 @@ static int amd_cache_northbridges(void) if (amd_gart_present()) amd_northbridges.flags |=3D AMD_NB_GART; =20 - /* - * Check for L3 cache presence. - */ - if (!cpuid_edx(0x80000006)) + if (!cpuid_amd_hygon_has_l3_cache()) return 0; =20 /* diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index e0d531e27ff3..71587570705f 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -281,29 +281,29 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *= c) return i; } =20 +/* + * AMD/Hygon CPUs may have multiple LLCs if L3 caches exist. + */ + void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u16 die_id) { - /* - * We may have multiple LLCs if L3 caches exist, so check if we - * have an L3 cache by looking at the L3 cache CPUID leaf. - */ - if (!cpuid_edx(0x80000006)) + if (!cpuid_amd_hygon_has_l3_cache()) return; =20 if (c->x86 < 0x17) { - /* LLC is at the node level. */ + /* Pre-Zen: LLC is at the node level */ c->topo.llc_id =3D die_id; } else if (c->x86 =3D=3D 0x17 && c->x86_model <=3D 0x1F) { /* - * LLC is at the core complex level. - * Core complex ID is ApicId[3] for these processors. + * Family 17h up to 1F models: LLC is at the core + * complex level. Core complex ID is ApicId[3]. */ c->topo.llc_id =3D c->topo.apicid >> 3; } else { /* - * LLC ID is calculated from the number of threads sharing the - * cache. - * */ + * Newer families: LLC ID is calculated from the number + * of threads sharing the L3 cache. + */ u32 eax, ebx, ecx, edx, num_sharing_cache =3D 0; u32 llc_index =3D find_num_cache_leaves(c) - 1; =20 @@ -321,16 +321,12 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c,= u16 die_id) =20 void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c) { - /* - * We may have multiple LLCs if L3 caches exist, so check if we - * have an L3 cache by looking at the L3 cache CPUID leaf. - */ - if (!cpuid_edx(0x80000006)) + if (!cpuid_amd_hygon_has_l3_cache()) return; =20 /* - * LLC is at the core complex level. - * Core complex ID is ApicId[3] for these processors. + * Hygons are similar to AMD Family 17h up to 1F models: LLC is + * at the core complex level. Core complex ID is ApicId[3]. */ c->topo.llc_id =3D c->topo.apicid >> 3; } --=20 2.48.1 From nobody Wed Dec 17 05:28:09 2025 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 5A69725C71D for ; Wed, 19 Mar 2025 12:23:14 +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=1742386996; cv=none; b=fH8jvUA+jgazIdZBKGr5xEHRrVawxH9dZjPrFgOfAYOEc6R6j+rWlZLiqMVEddBCKjifSy1I9nrXjhIJ5nuJ9oNLh3HUAPLPxcKM+soq9A0JCNYDw+2PN4to1BvgxtV0vIMd4aKd1RybmcYFyKVM6OhxyvtvuWuG5oNxr17Gkpk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742386996; c=relaxed/simple; bh=O5fwoGUir2sJrl4CT0DSG4QJbscmWRWltiKSUfcWM6E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gvpC3i51TeKrXwME+/87yIqFBsLeWdfkaTAASPv87y6bSTzHwa4Kp09JsgFFoh69r9r6TL5nl0/omao708DO5+ukCFrL8EW/sEdWRwQW9l32IOVA/Ek681kcdP/cJyURn5HCZcdteR1BRnwImsKZyMSyLPjfR9goajPh9rSTYHg= 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=sdNeHxqm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=EYwMMjii; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sdNeHxqm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="EYwMMjii" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742386993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WsDNajMuLUSUWVFOMt+H94Y8ummXa+oY4OeyGeOksL0=; b=sdNeHxqmxbUBHsS5C2XoDeL8S30ObWA60Q2T/pOVe5mk5H00L30kRMqGPT4z5sMc7yg+d0 xlxoBL7qAzHi6WJjKIV570U4c6l9WXWH4Pzz7nHDXd+0WZBD2+RQsgHVIZBBD1jQA9btGi DF83XE8eR4lkBNycQKAEPCpzy+meCTqXzZW2WDzLi17RwRi/yaP5/7xXOBLoUGszAKnlEu hO35b13KeuhJEsvTkgdeRvtOoIHmG78jhHOGYKQfKhoPmuPRrcTV4ZwVW+AbPtusuFaX69 DdeYcWZEzf8Kk4m6/9w5CrX9X8H/kn2oEiKHRFENpaJkCth38i6dGlrD/rxTtw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742386993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WsDNajMuLUSUWVFOMt+H94Y8ummXa+oY4OeyGeOksL0=; b=EYwMMjiif7RzObqNhX+P4W5FncXRwbkCxbzoXxwuFYjBEqizfg1tIDkzj1vMWT+ekvYwM2 1FJckY4UB+7VsFBQ== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 29/29] x86/cacheinfo: Apply maintainer-tip coding style fixes Date: Wed, 19 Mar 2025 13:21:37 +0100 Message-ID: <20250319122137.4004-30-darwi@linutronix.de> In-Reply-To: <20250319122137.4004-1-darwi@linutronix.de> References: <20250319122137.4004-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The x86/cacheinfo code has been heavily refactored and fleshed out at parent commits, where any necessary coding style fixes were also done in place. Apply Documentation/process/maintainer-tip.rst coding style fixes to the rest of the code, and align its assignment expressions for readability. Standardize on CPUID(n) when mentioning leaf queries. Avoid breaking long lines when doing so helps readability. At cacheinfo_amd_init_llc_id(), rename variable 'msb' to 'index_msb' as this is how it's called at the rest of cacheinfo.c code. Signed-off-by: Ahmed S. Darwish --- arch/x86/kernel/cpu/cacheinfo.c | 215 ++++++++++++++++---------------- 1 file changed, 107 insertions(+), 108 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinf= o.c index 71587570705f..cd48d34ac04b 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -1,11 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 /* - * Routines to identify caches on Intel CPU. + * x86 CPU caches detection and configuration * - * Changes: - * Venkatesh Pallipadi : Adding cache identification through cpuid(4) - * Ashok Raj : Work with CPU hotplug infrastructure. - * Andi Kleen / Andreas Herrmann : CPUID4 emulation on AMD. + * Previous changes + * - Venkatesh Pallipadi: Cache identification through CPUID(4) + * - Ashok Raj : Work with CPU hotplug infrastructure + * - Andi Kleen / Andreas Herrmann: CPUID(4) emulation on AMD */ =20 #include @@ -35,37 +35,37 @@ static cpumask_var_t cpu_cacheinfo_mask; unsigned int memory_caching_control __ro_after_init; =20 enum _cache_type { - CTYPE_NULL =3D 0, - CTYPE_DATA =3D 1, - CTYPE_INST =3D 2, - CTYPE_UNIFIED =3D 3 + CTYPE_NULL =3D 0, + CTYPE_DATA =3D 1, + CTYPE_INST =3D 2, + CTYPE_UNIFIED =3D 3 }; =20 union _cpuid4_leaf_eax { struct { - enum _cache_type type:5; - unsigned int level:3; - unsigned int is_self_initializing:1; - unsigned int is_fully_associative:1; - unsigned int reserved:4; - unsigned int num_threads_sharing:12; - unsigned int num_cores_on_die:6; + enum _cache_type type :5; + unsigned int level :3; + unsigned int is_self_initializing :1; + unsigned int is_fully_associative :1; + unsigned int reserved :4; + unsigned int num_threads_sharing :12; + unsigned int num_cores_on_die :6; } split; u32 full; }; =20 union _cpuid4_leaf_ebx { struct { - unsigned int coherency_line_size:12; - unsigned int physical_line_partition:10; - unsigned int ways_of_associativity:10; + unsigned int coherency_line_size :12; + unsigned int physical_line_partition :10; + unsigned int ways_of_associativity :10; } split; u32 full; }; =20 union _cpuid4_leaf_ecx { struct { - unsigned int number_of_sets:32; + unsigned int number_of_sets :32; } split; u32 full; }; @@ -93,60 +93,59 @@ static const enum cache_type cache_type_map[] =3D { =20 union l1_cache { struct { - unsigned line_size:8; - unsigned lines_per_tag:8; - unsigned assoc:8; - unsigned size_in_kb:8; + unsigned line_size :8; + unsigned lines_per_tag :8; + unsigned assoc :8; + unsigned size_in_kb :8; }; - unsigned val; + unsigned int val; }; =20 union l2_cache { struct { - unsigned line_size:8; - unsigned lines_per_tag:4; - unsigned assoc:4; - unsigned size_in_kb:16; + unsigned line_size :8; + unsigned lines_per_tag :4; + unsigned assoc :4; + unsigned size_in_kb :16; }; - unsigned val; + unsigned int val; }; =20 union l3_cache { struct { - unsigned line_size:8; - unsigned lines_per_tag:4; - unsigned assoc:4; - unsigned res:2; - unsigned size_encoded:14; + unsigned line_size :8; + unsigned lines_per_tag :4; + unsigned assoc :4; + unsigned res :2; + unsigned size_encoded :14; }; - unsigned val; + unsigned int val; }; =20 static const unsigned short assocs[] =3D { - [1] =3D 1, - [2] =3D 2, - [4] =3D 4, - [6] =3D 8, - [8] =3D 16, - [0xa] =3D 32, - [0xb] =3D 48, - [0xc] =3D 64, - [0xd] =3D 96, - [0xe] =3D 128, - [0xf] =3D 0xffff /* fully associative - no way to show this currently */ + [1] =3D 1, + [2] =3D 2, + [4] =3D 4, + [6] =3D 8, + [8] =3D 16, + [0xa] =3D 32, + [0xb] =3D 48, + [0xc] =3D 64, + [0xd] =3D 96, + [0xe] =3D 128, + [0xf] =3D 0xffff /* Fully associative */ }; =20 static const unsigned char levels[] =3D { 1, 1, 2, 3 }; -static const unsigned char types[] =3D { 1, 2, 3, 3 }; +static const unsigned char types[] =3D { 1, 2, 3, 3 }; =20 static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx) { unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb; - union l1_cache l1i, l1d; + union l1_cache l1i, l1d, *l1; union l2_cache l2; union l3_cache l3; - union l1_cache *l1 =3D &l1d; =20 eax->full =3D 0; ebx->full =3D 0; @@ -155,6 +154,7 @@ static void legacy_amd_cpuid4(int index, union _cpuid4_= leaf_eax *eax, cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val); cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val); =20 + l1 =3D &l1d; switch (index) { case 1: l1 =3D &l1i; @@ -162,48 +162,52 @@ static void legacy_amd_cpuid4(int index, union _cpuid= 4_leaf_eax *eax, case 0: if (!l1->val) return; - assoc =3D assocs[l1->assoc]; - line_size =3D l1->line_size; - lines_per_tag =3D l1->lines_per_tag; - size_in_kb =3D l1->size_in_kb; + + assoc =3D assocs[l1->assoc]; + line_size =3D l1->line_size; + lines_per_tag =3D l1->lines_per_tag; + size_in_kb =3D l1->size_in_kb; break; case 2: if (!l2.val) return; - assoc =3D assocs[l2.assoc]; - line_size =3D l2.line_size; - lines_per_tag =3D l2.lines_per_tag; - /* cpu_data has errata corrections for K7 applied */ - size_in_kb =3D __this_cpu_read(cpu_info.x86_cache_size); + + /* Use x86_cache_size as it might have K7 errata fixes */ + assoc =3D assocs[l2.assoc]; + line_size =3D l2.line_size; + lines_per_tag =3D l2.lines_per_tag; + size_in_kb =3D __this_cpu_read(cpu_info.x86_cache_size); break; case 3: if (!l3.val) return; - assoc =3D assocs[l3.assoc]; - line_size =3D l3.line_size; - lines_per_tag =3D l3.lines_per_tag; - size_in_kb =3D l3.size_encoded * 512; + + assoc =3D assocs[l3.assoc]; + line_size =3D l3.line_size; + lines_per_tag =3D l3.lines_per_tag; + size_in_kb =3D l3.size_encoded * 512; if (boot_cpu_has(X86_FEATURE_AMD_DCM)) { - size_in_kb =3D size_in_kb >> 1; - assoc =3D assoc >> 1; + size_in_kb =3D size_in_kb >> 1; + assoc =3D assoc >> 1; } break; default: return; } =20 - eax->split.is_self_initializing =3D 1; - eax->split.type =3D types[index]; - eax->split.level =3D levels[index]; - eax->split.num_threads_sharing =3D 0; - eax->split.num_cores_on_die =3D topology_num_cores_per_package(); + eax->split.is_self_initializing =3D 1; + eax->split.type =3D types[index]; + eax->split.level =3D levels[index]; + eax->split.num_threads_sharing =3D 0; + eax->split.num_cores_on_die =3D topology_num_cores_per_package(); =20 if (assoc =3D=3D 0xffff) eax->split.is_fully_associative =3D 1; - ebx->split.coherency_line_size =3D line_size - 1; - ebx->split.ways_of_associativity =3D assoc - 1; - ebx->split.physical_line_partition =3D lines_per_tag - 1; - ecx->split.number_of_sets =3D (size_in_kb * 1024) / line_size / + + ebx->split.coherency_line_size =3D line_size - 1; + ebx->split.ways_of_associativity =3D assoc - 1; + ebx->split.physical_line_partition =3D lines_per_tag - 1; + ecx->split.number_of_sets =3D (size_in_kb * 1024) / line_size / (ebx->split.ways_of_associativity + 1) - 1; } =20 @@ -262,19 +266,14 @@ static int fill_cpuid4_info(int index, struct _cpuid4= _info *id4) =20 static int find_num_cache_leaves(struct cpuinfo_x86 *c) { - unsigned int eax, ebx, ecx, edx, op; - union _cpuid4_leaf_eax cache_eax; - int i =3D -1; - - if (c->x86_vendor =3D=3D X86_VENDOR_AMD || - c->x86_vendor =3D=3D X86_VENDOR_HYGON) - op =3D 0x8000001d; - else - op =3D 4; + unsigned int eax, ebx, ecx, edx, op; + union _cpuid4_leaf_eax cache_eax; + int i =3D -1; =20 + /* Do a CPUID(op) loop to calculate num_cache_leaves */ + op =3D (c->x86_vendor =3D=3D X86_VENDOR_AMD || c->x86_vendor =3D=3D X86_V= ENDOR_HYGON) ? 0x8000001d : 4; do { ++i; - /* Do cpuid(op) loop to find out num_cache_leaves */ cpuid_count(op, i, &eax, &ebx, &ecx, &edx); cache_eax.full =3D eax; } while (cache_eax.split.type !=3D CTYPE_NULL); @@ -312,9 +311,9 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u= 16 die_id) num_sharing_cache =3D ((eax >> 14) & 0xfff) + 1; =20 if (num_sharing_cache) { - int bits =3D get_count_order(num_sharing_cache); + int index_msb =3D get_count_order(num_sharing_cache); =20 - c->topo.llc_id =3D c->topo.apicid >> bits; + c->topo.llc_id =3D c->topo.apicid >> index_msb; } } } @@ -335,14 +334,10 @@ void init_amd_cacheinfo(struct cpuinfo_x86 *c) { struct cpu_cacheinfo *ci =3D get_cpu_cacheinfo(c->cpu_index); =20 - if (boot_cpu_has(X86_FEATURE_TOPOEXT)) { + if (boot_cpu_has(X86_FEATURE_TOPOEXT)) ci->num_leaves =3D find_num_cache_leaves(c); - } else if (c->extended_cpuid_level >=3D 0x80000006) { - if (cpuid_edx(0x80000006) & 0xf000) - ci->num_leaves =3D 4; - else - ci->num_leaves =3D 3; - } + else if (c->extended_cpuid_level >=3D 0x80000006) + ci->num_leaves =3D (cpuid_edx(0x80000006) & 0xf000) ? 4 : 3; } =20 void init_hygon_cacheinfo(struct cpuinfo_x86 *c) @@ -469,6 +464,9 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) intel_cacheinfo_0x2(c); } =20 +/* + * linux/cacheinfo.h shared_cpu_map setup, AMD/Hygon + */ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, const struct _cpuid4_info *id4) { @@ -485,12 +483,12 @@ static int __cache_amd_cpumap_setup(unsigned int cpu,= int index, this_cpu_ci =3D get_cpu_cacheinfo(i); if (!this_cpu_ci->info_list) continue; + ci =3D this_cpu_ci->info_list + index; for_each_cpu(sibling, cpu_llc_shared_mask(cpu)) { if (!cpu_online(sibling)) continue; - cpumask_set_cpu(sibling, - &ci->shared_cpu_map); + cpumask_set_cpu(sibling, &ci->shared_cpu_map); } } } else if (boot_cpu_has(X86_FEATURE_TOPOEXT)) { @@ -516,8 +514,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, i= nt index, apicid =3D cpu_data(sibling).topo.apicid; if ((apicid < first) || (apicid > last)) continue; - cpumask_set_cpu(sibling, - &ci->shared_cpu_map); + cpumask_set_cpu(sibling, &ci->shared_cpu_map); } } } else @@ -526,17 +523,19 @@ static int __cache_amd_cpumap_setup(unsigned int cpu,= int index, return 1; } =20 +/* + * linux/cacheinfo.h shared_cpu_map setup, Intel + fallback AMD/Hygon + */ static void __cache_cpumap_setup(unsigned int cpu, int index, const struct _cpuid4_info *id4) { struct cpu_cacheinfo *this_cpu_ci =3D get_cpu_cacheinfo(cpu); + struct cpuinfo_x86 *c =3D &cpu_data(cpu); struct cacheinfo *ci, *sibling_ci; unsigned long num_threads_sharing; int index_msb, i; - struct cpuinfo_x86 *c =3D &cpu_data(cpu); =20 - if (c->x86_vendor =3D=3D X86_VENDOR_AMD || - c->x86_vendor =3D=3D X86_VENDOR_HYGON) { + if (c->x86_vendor =3D=3D X86_VENDOR_AMD || c->x86_vendor =3D=3D X86_VENDO= R_HYGON) { if (__cache_amd_cpumap_setup(cpu, index, id4)) return; } @@ -554,8 +553,10 @@ static void __cache_cpumap_setup(unsigned int cpu, int= index, if (cpu_data(i).topo.apicid >> index_msb =3D=3D c->topo.apicid >> index_= msb) { struct cpu_cacheinfo *sib_cpu_ci =3D get_cpu_cacheinfo(i); =20 + /* Skip if itself or no cacheinfo */ if (i =3D=3D cpu || !sib_cpu_ci->info_list) - continue;/* skip if itself or no cacheinfo */ + continue; + sibling_ci =3D sib_cpu_ci->info_list + index; cpumask_set_cpu(i, &ci->shared_cpu_map); cpumask_set_cpu(cpu, &sibling_ci->shared_cpu_map); @@ -589,7 +590,7 @@ int init_cache_level(unsigned int cpu) } =20 /* - * The max shared threads number comes from CPUID.4:EAX[25-14] with input + * The max shared threads number comes from CPUID(4) EAX[25-14] with input * ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */ @@ -626,8 +627,8 @@ int populate_cache_leaves(unsigned int cpu) ci_info_init(ci++, &id4, nb); __cache_cpumap_setup(cpu, idx, &id4); } - this_cpu_ci->cpu_map_populated =3D true; =20 + this_cpu_ci->cpu_map_populated =3D true; return 0; } =20 @@ -659,12 +660,10 @@ void cache_disable(void) __acquires(cache_disable_loc= k) unsigned long cr0; =20 /* - * Note that this is not ideal - * since the cache is only flushed/disabled for this CPU while the - * MTRRs are changed, but changing this requires more invasive - * changes to the way the kernel boots + * This is not ideal since the cache is only flushed/disabled + * for this CPU while the MTRRs are changed, but changing this + * requires more invasive changes to the way the kernel boots. */ - raw_spin_lock(&cache_disable_lock); =20 /* Enter the no-fill (CD=3D1, NW=3D0) cache mode and flush caches. */ --=20 2.48.1