From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 774A73FF1D9 for ; Fri, 29 May 2026 14:45:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065917; cv=none; b=UFiDuh7xforPZkZBlhinRgBmonMEfXrhMIvCDkFsJcPRMmFZMzAYeA524FuVHYOlahyeJHHg+BHi6tR4FJezTCO5CohnPwEcz+YgZfWfb6gghYonLQwYguXRKst3VXAzsL2yWdnAjg6XexlGDu1tjDWb9dzCpe1XfUm0p0Ixo2w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065917; c=relaxed/simple; bh=KyB0Txj56NS/h0Juiajq3RnH9IfQJxHtRnFzyefeuUA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CZM+L874vG0zg6hkIPip2JLBJ6HLLKFTCa8pKnLvvpWy/zbxIUCelaOrQWJEej7Pbm+rIlzvyIglHcUVEnp1Vxy180Ga9UkfVwEysyIDREWHXHTQ3r7jJH52TNBhifVam2MUuOKd3uDXaSp5HC8fwbD2TyurwamxWJDrQZ/FNwM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WMJW5xzp; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WMJW5xzp" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf11699875so19125215ad.1 for ; Fri, 29 May 2026 07:45:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780065915; x=1780670715; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=33AEblmPZenCG7ncxIbBQYlPZl7MWXLQcjQTaHUg1Zw=; b=WMJW5xzpa6NMsaN0ORq9dS4MPkradsAf8wJJ6A7PvAUZtv0iYY+YaViDYrv18YH6tR 4AP5XrvgSTuJKpHU1imrIOh7YdbiAMoTyReV/+RfP192lV0UKCdoqjqH4k9Z5BViLs1b 1aEBr/omLDLhr2Y6qD3UUqtk8Qfn5xNCGKd1js51sc39utlDfRAS9sbankb3oOmG4SYf /TyO7BpxKtdiGrAeQJTuF2kcOQfj5iBIvRYlYsPFeUvxZl4Tj9T+U+5L6x8PhIo2mPkk 258waLs/6btaK/M9IrCq67qRYM2/g1NMHb/FlrmgVGQUC7IUWd5PRN7C71MOjQEP+eNp MOQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780065915; x=1780670715; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=33AEblmPZenCG7ncxIbBQYlPZl7MWXLQcjQTaHUg1Zw=; b=PU4WvmTpTIpfRugqO31YiMOUQ9RDIeJ/5JiOrKS3L+gT2Fu9cIsmHaZmsJxmgzbYQ9 woI/F7pDvXO2Rvt6BQwLy75eSXmrQD1daCN+bbnAqRujwOZJeRZ0OawLWrA68E03mPn7 RVgv9DIUEQu9mXGoPOzEiP57MsHmI80XjrbNE30I4oSwwLWbq0A3QKzG9N/Bqq3E+t3s 0gPPC/em0OrwhmZwriNQ/q5DdOmhaju4OkUJujZKdxXx279QavhNG7XDUvbpvZrucVi9 eYE9tPxW9Ab4JCa3CAwZ40yaRXp7InsvCeRyZ6glezGmrtGv0DmIiKACKgoCRQUAvRWQ j9zA== X-Forwarded-Encrypted: i=1; AFNElJ8/6Y1HybwL4dPXxDu5O9KPHbVgBTxsJ0IdfbY9qVyaL9tPQaGgAmVYOf9PkJuDasU9JXqST2hSLFw1DgQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzLs85CIqWJ0jrPsnmlI9jSpe+QEgcFp5Numc05C90uVcwC0+MQ 4JmNYpY0j81lcxRzgnJLqfo77/ZY158c8y8rCdrwL2jkO3+5sPvEoHNhWmvwpsjqxf3bFilGg1S OoYRDjQ== X-Received: from plaq13.prod.google.com ([2002:a17:903:204d:b0:2bf:68:53db]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ffcb:b0:2b9:cabe:ce37 with SMTP id d9443c01a7336-2bf204679c2mr35097225ad.1.1780065914467; Fri, 29 May 2026 07:45:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 07:44:06 -0700 In-Reply-To: <20260529144435.704127-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529144435.704127-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529144435.704127-20-seanjc@google.com> Subject: [PATCH v4 19/47] x86/tsc: Add standalone helper for getting CPU frequency from CPUID From: Sean Christopherson To: Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Sean Christopherson , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , John Stultz Cc: "H. Peter Anvin" , Rick Edgecombe , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, xen-devel@lists.xenproject.org, David Woodhouse , Tom Lendacky , Nikunj A Dadhania , David Woodhouse , Michael Kelley , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Extract the guts of cpu_khz_from_cpuid() to a standalone helper that doesn't restrict the usage to Intel CPUs. This will allow sharing the core logic with KVM-as-a-guest, as KVM generally doesn't restrict CPUID based on vendor. No functional change intended. Reviewed-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/tsc.h | 1 + arch/x86/kernel/tsc.c | 32 +++++++++++++++----------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h index 4a224f99c3b9..7ff2bfdcdf38 100644 --- a/arch/x86/include/asm/tsc.h +++ b/arch/x86/include/asm/tsc.h @@ -90,6 +90,7 @@ struct cpuid_tsc_info { unsigned int tsc_khz; }; extern int __init cpuid_get_tsc_freq(struct cpuid_tsc_info *info); +extern unsigned int __cpu_khz_from_cpuid(void); extern void tsc_early_init(void); extern void tsc_init(void); diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 3e911f0f7364..bdff8c988866 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -692,6 +692,18 @@ int __init cpuid_get_tsc_freq(struct cpuid_tsc_info *info) return 0; } +unsigned int __cpu_khz_from_cpuid(void) +{ + unsigned int eax_base_mhz, ebx, ecx, edx; + + if (boot_cpu_data.cpuid_level < CPUID_LEAF_FREQ) + return 0; + + cpuid(CPUID_LEAF_FREQ, &eax_base_mhz, &ebx, &ecx, &edx); + + return eax_base_mhz * 1000; +} + /** * native_calibrate_tsc - determine TSC frequency * Determine TSC frequency via CPUID, else return 0. @@ -727,13 +739,8 @@ static unsigned long native_calibrate_tsc(void) * clock, but we can easily calculate it to a high degree of accuracy * by considering the crystal ratio and the CPU speed. */ - if (!info.crystal_khz && boot_cpu_data.cpuid_level >= CPUID_LEAF_FREQ) { - unsigned int eax_base_mhz, ebx, ecx, edx; - - cpuid(CPUID_LEAF_FREQ, &eax_base_mhz, &ebx, &ecx, &edx); - info.crystal_khz = eax_base_mhz * 1000 * - info.denominator / info.numerator; - } + if (!info.crystal_khz) + info.crystal_khz = __cpu_khz_from_cpuid() * info.denominator / info.numerator; if (!info.crystal_khz) return 0; @@ -760,19 +767,10 @@ static unsigned long native_calibrate_tsc(void) static unsigned long cpu_khz_from_cpuid(void) { - unsigned int eax_base_mhz, ebx_max_mhz, ecx_bus_mhz, edx; - if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) return 0; - if (boot_cpu_data.cpuid_level < CPUID_LEAF_FREQ) - return 0; - - eax_base_mhz = ebx_max_mhz = ecx_bus_mhz = edx = 0; - - cpuid(CPUID_LEAF_FREQ, &eax_base_mhz, &ebx_max_mhz, &ecx_bus_mhz, &edx); - - return eax_base_mhz * 1000; + return __cpu_khz_from_cpuid(); } /* -- 2.54.0.823.g6e5bcc1fc9-goog