Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1394735lqp; Fri, 22 Mar 2024 13:35:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVuvq93JGob8/wXKCUjzNBcNXZHPpl4kmQ5fYpQfNU1+RgFTVWlnAzu3WvnrC2k90PvETvXjTC4MqC0hBLg/FehWw1fsX8lKVLMg+YvLQ== X-Google-Smtp-Source: AGHT+IH4OSE3Evyt/gDKO1acOxMizC79+771EhlWZlxCTssggy/pdMKqXW6jkQ+Yn9Rp6shRVLOK X-Received: by 2002:a05:6358:2813:b0:17e:8b66:a983 with SMTP id k19-20020a056358281300b0017e8b66a983mr530192rwb.21.1711139709554; Fri, 22 Mar 2024 13:35:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711139709; cv=pass; d=google.com; s=arc-20160816; b=knHb8uYqKmM7/CLxNboilqRgsg8mLXPGeNnJqfG7I8JpSZo0V1WEjgMo1b2AO7hIk7 gucq/v9xQ+2qUH7IKfc91rI3SyPGEx2SK0ccYEwUCs8X05wKCAmuy3GGf424aT0akasR mNPvjfuKfyBccI/4l9rEZqXQ14ZEcFTUUJRhoagxFSo/uka8FdcbSPLz+Cd5Hw7jMhGb lppCBDb+6hI8sbD4q3h8hNau+gzPmcCef/tbHSbs0E1RDfj887934wlT0Ix7/U/PX22k aEOcnCxhGSdQqJjK+EfmdPxDU1c5S0NFb4CsfCb12VJ6+sYUQ3Xzx4qo1iFJ75v+mFKc D5PQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:message-id :in-reply-to:references:date:from:cc:to:subject:dkim-signature; bh=gGF6LAqv1dmG6tR7DB8sUp7nb+zjFyjVjEwaOhXA5Mw=; fh=PELvsQW1nNjYeAshJzzD7rCh1mAO7EHXrR7AF8tavkc=; b=CB+6ewl7UateVi/X/MJtsRplSXlL1rCKVDGF9H+3AtduoClYgB8q+B3kPpfPTPQv8s 4NV991JDUURqG0PBxqq5dcgMTc2z4UQxvRpqSKcFjX2zpJwYEryUeNc+T83xMcFp67o2 OrajmqIiE1/8ppRG8h2UErUHcQQk3SupfXrC5Fqkx3mhW28m2m7JHFFTQW9SA5vTZ7HZ bTBvfYzOsOE+/Xgd76MRESYryzlFsXSldb9mPcOPHmCSIZnHS2MjEgvX8gkIEilx3MoK TNzTPO62zrjNg8cpjlcv0omSuJ/uoH7zNxpPUpVDosOzolsypeIAcEfDSYV94bXsY8Sq lp0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Hg0oIPa3; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-111896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111896-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id p30-20020a63951e000000b005dc8368c55esi2776292pgd.633.2024.03.22.13.35.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 13:35:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Hg0oIPa3; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-111896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111896-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2CC35B217ED for ; Fri, 22 Mar 2024 17:56:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B571060DCC; Fri, 22 Mar 2024 17:56:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Hg0oIPa3" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (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 19FC260894 for ; Fri, 22 Mar 2024 17:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711130194; cv=none; b=PJmtF2Ql9UKI+KY4kK36KqaazVsmhBSJdbsb1cML7adBNTGn5BBFRsm0rKMKecUauiFyf1KbtySzB1wafTusksSzdmHP13CsNEmySTwMrUmH2JxyU5Tr/ux2v22rhzhEXFkoYQlNOY1q2nFQxYtnMVv7apS6MfLr+1bHhNuNMbg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711130194; c=relaxed/simple; bh=zpniM12Plr/IbgqsKzUJwGxiwaeLzWobVxGgT3mo4kk=; h=Subject:To:Cc:From:Date:References:In-Reply-To:Message-Id; b=btbH+a3N+BVVwrx1pWTF96BRK/7VRSuZups3un3ABepzKqbefDoIRzf4rrdWT4Z0wt/FI/maoyaJJzd2LZ+MbdefYhAmbG1rmkKnNrWS3DCqxKxfBo16d5m2mKCS56euY/uI07ytIKfHyVg13iOnfJylddynHLFwFqhmsE+UUbc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Hg0oIPa3; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711130193; x=1742666193; h=subject:to:cc:from:date:references:in-reply-to: message-id; bh=zpniM12Plr/IbgqsKzUJwGxiwaeLzWobVxGgT3mo4kk=; b=Hg0oIPa3kT9l1QWxL61BZfhiYwiC+RdYu5X3zuYZjcBUkFpDnvUI3+C/ HeU8tNzYDwRG7UI5m8qXBSZsbbcQDL3U29zwttp26Fu75p0TN5tB5xMs+ pB2jWKSGoGuwhKpseTUp+Bn7tMyZM+YIfRv3DAHyBgZTe2XNoyiOny1Ud FtHYN2i3Kc2rnrsr0iex8n0SRxXuoo4sImmf7++N/zcqgJlezp5si/wDG QS0n4xkMUHI+9l7rdXCYgDyB7OhtammThgCtQwEujYtNMg0j8cqC0Yibs XpyI0Gjlv7J95HQFYNMLhzT8hrwE7mV+/lquhke+6iXFHmfqFwsJK0ABd Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11020"; a="28669679" X-IronPort-AV: E=Sophos;i="6.07,146,1708416000"; d="scan'208";a="28669679" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Mar 2024 10:56:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,146,1708416000"; d="scan'208";a="15425569" Received: from davehans-spike.ostc.intel.com (HELO localhost.localdomain) ([10.165.164.11]) by orviesa006.jf.intel.com with ESMTP; 22 Mar 2024 10:56:31 -0700 Subject: [PATCH 1/4] x86/cpu: Add and use new CPUID region helper To: linux-kernel@vger.kernel.org Cc: jgross@suse.com,tglx@linutronix.de,x86@kernel.org,bp@alien8.de,Dave Hansen From: Dave Hansen Date: Fri, 22 Mar 2024 10:56:30 -0700 References: <20240322175629.01E8B39D@davehans-spike.ostc.intel.com> In-Reply-To: <20240322175629.01E8B39D@davehans-spike.ostc.intel.com> Message-Id: <20240322175630.72CE974F@davehans-spike.ostc.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: Dave Hansen There are some (before now) unwritten rules about CPUID "regions". Basically, there is a 32-bit address space of CPUID leaves. The top 16 bits address a "region" and the first leaf in a region is special. The kernel only has a few spots that care about this, but it's rather hard to make sense of the code as is. Add a helper that explains regions. Use it where applicable. Signed-off-by: Dave Hansen Cc: Juergen Gross --- b/arch/x86/include/asm/cpuid.h | 59 ++++++++++++++++++++++++++++++++++++++ b/arch/x86/kernel/cpu/common.c | 13 +++----- b/arch/x86/kernel/cpu/transmeta.c | 9 +---- b/arch/x86/xen/enlighten_pv.c | 9 +---- 4 files changed, 69 insertions(+), 21 deletions(-) diff -puN arch/x86/include/asm/cpuid.h~cpuid-regions arch/x86/include/asm/cpuid.h --- a/arch/x86/include/asm/cpuid.h~cpuid-regions 2024-03-18 15:12:20.676308753 -0700 +++ b/arch/x86/include/asm/cpuid.h 2024-03-22 09:17:13.296507986 -0700 @@ -168,4 +168,63 @@ static inline uint32_t hypervisor_cpuid_ return 0; } +/* + * By convention, CPUID is broken up into regions which each + * have 2^16 leaves. EAX in the first leaf of each valid + * region returns the maximum valid leaf in that region. + * + * The regions can be thought of as being vendor-specific + * areas of CPUID, but that's imprecise because everybody + * implements the "Intel" region and Intel implements the + * AMD region. There are a few well-known regions: + * - Intel (0x0000) + * - AMD (0x8000) + * - Transmeta (0x8086) + * - Centaur (0xC000) + * + * Consider a CPU that where the maximum leaf in the Transmeta + * region is 2. On such a CPU, leaf 0x80860000 would contain: + * EAX==0x80860002. + * region-^^^^ + * max leaf-^^^^ + */ +static inline u32 cpuid_region_max_leaf(u16 region) +{ + u32 eax = cpuid_eax(region << 16); + + /* + * An unsupported region may return data from the last + * "basic" leaf, which is essentially garbage. Avoid + * mistaking basic leaf data for region data. + * + * Note: this is not perfect. It is theoretically + * possible for the last basic leaf to _resemble_ a + * valid first leaf from a region that doesn't exist. + * But Intel at least seems to pad out the basic region + * with 0's, possibly to avoid this. + */ + if ((eax >> 16) != region) + return 0; + + return eax; +} + +/* Returns true if the leaf exists and @value was populated */ +static inline bool get_cpuid_region_leaf(u32 leaf, enum cpuid_regs_idx reg, + u32 *value) +{ + u16 region = leaf >> 16; + u32 regs[4]; + + if (cpuid_region_max_leaf(region) < leaf) + return false; + + cpuid(leaf, ®s[CPUID_EAX], ®s[CPUID_EBX], + ®s[CPUID_ECX], ®s[CPUID_EDX]); + + *value = regs[reg]; + + return true; +} + #endif /* _ASM_X86_CPUID_H */ diff -puN arch/x86/kernel/cpu/common.c~cpuid-regions arch/x86/kernel/cpu/common.c --- a/arch/x86/kernel/cpu/common.c~cpuid-regions 2024-03-18 15:12:20.676308753 -0700 +++ b/arch/x86/kernel/cpu/common.c 2024-03-18 15:12:20.684309023 -0700 @@ -1049,16 +1049,13 @@ void get_cpu_cap(struct cpuinfo_x86 *c) } /* AMD-defined flags: level 0x80000001 */ - eax = cpuid_eax(0x80000000); - c->extended_cpuid_level = eax; + c->extended_cpuid_level = cpuid_region_max_leaf(0x8000); - if ((eax & 0xffff0000) == 0x80000000) { - if (eax >= 0x80000001) { - cpuid(0x80000001, &eax, &ebx, &ecx, &edx); + if (c->extended_cpuid_level >= 0x80000001) { + cpuid(0x80000001, &eax, &ebx, &ecx, &edx); - c->x86_capability[CPUID_8000_0001_ECX] = ecx; - c->x86_capability[CPUID_8000_0001_EDX] = edx; - } + c->x86_capability[CPUID_8000_0001_ECX] = ecx; + c->x86_capability[CPUID_8000_0001_EDX] = edx; } if (c->extended_cpuid_level >= 0x80000007) { diff -puN arch/x86/kernel/cpu/transmeta.c~cpuid-regions arch/x86/kernel/cpu/transmeta.c --- a/arch/x86/kernel/cpu/transmeta.c~cpuid-regions 2024-03-18 15:12:20.680308889 -0700 +++ b/arch/x86/kernel/cpu/transmeta.c 2024-03-18 15:12:20.684309023 -0700 @@ -9,14 +9,9 @@ static void early_init_transmeta(struct cpuinfo_x86 *c) { - u32 xlvl; - /* Transmeta-defined flags: level 0x80860001 */ - xlvl = cpuid_eax(0x80860000); - if ((xlvl & 0xffff0000) == 0x80860000) { - if (xlvl >= 0x80860001) - c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001); - } + get_cpuid_region_leaf(0x80860001, CPUID_EDX, + &c->x86_capability[CPUID_8086_0001_EDX]); } static void init_transmeta(struct cpuinfo_x86 *c) diff -puN arch/x86/xen/enlighten_pv.c~cpuid-regions arch/x86/xen/enlighten_pv.c --- a/arch/x86/xen/enlighten_pv.c~cpuid-regions 2024-03-18 15:12:20.680308889 -0700 +++ b/arch/x86/xen/enlighten_pv.c 2024-03-22 09:15:33.280428290 -0700 @@ -141,16 +141,13 @@ static void __init xen_set_mtrr_data(voi }; unsigned int reg; unsigned long mask; - uint32_t eax, width; + uint32_t width; static struct mtrr_var_range var[MTRR_MAX_VAR_RANGES] __initdata; /* Get physical address width (only 64-bit cpus supported). */ width = 36; - eax = cpuid_eax(0x80000000); - if ((eax >> 16) == 0x8000 && eax >= 0x80000008) { - eax = cpuid_eax(0x80000008); - width = eax & 0xff; - } + /* Will overwrite 'width' if available in CPUID: */ + get_cpuid_region_leaf(0x80000008, CPUID_EAX, &width); for (reg = 0; reg < MTRR_MAX_VAR_RANGES; reg++) { op.u.read_memtype.reg = reg; _