From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 D2B004A1CBB for ; Thu, 6 Aug 2026 23:36:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059417; cv=none; b=PzFWx1tFszP/YdiEto7/n8L91oUDUbAS1/aEdNbtX+ecjVGzz4s/1mhMHfdwRFHa3pmyPCXnnmYeaXVEhcreqm2BS3HdUTqs7bngmeOM/XDFtXflGKsMRlp1i2Hk5H0hwCqd+SqXiNmzBmjcH9SolYkBK9KJvNF+1pfprYmZXpk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059417; c=relaxed/simple; bh=4ILgpsXZ3J+MDpjYL5bz0ooI2SmcUBqM1FeGA0GNQQ4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N4VINEtjl23esQD6QoZajZNrAIEAIVa7LHQLrNKKs3LtzQ2OQsEHhCCBCfAODBdq8L640yufs7G8zaMK3HkHbveCuXzzwjQyAzg56tNf+reQk+LERwnPfEC8qKpV08EHHgLLM7/IXkg1pWZ5oa14kXBp6DxC3vAZ5cVNNCMfNHA= 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=o+cjOMv8; arc=none smtp.client-ip=209.85.215.198 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="o+cjOMv8" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cb5bdd8450dso1769447a12.0 for ; Thu, 06 Aug 2026 16:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786059413; x=1786664213; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=gK3sjM001OyHimEt3r9e3MTxT5BkhagjBxMzBV7doNU=; b=o+cjOMv85pq6SxqpSknjaw3cf3ahvB6P1WvdMIQjEdOYpPYNSmAtZ7+Gmr63usDs1F 6563QkMxQ0l2AOCQ3ozQlXupjfuLKrosrgQyiJ6KLyuHLEMaOsiLRETcUzD1Pxdxic9M Cxag6igEzadap6CjAziWr3pymmMWZexdRkNk88DMltCopf3/00+fE5YauJMDRvQoc4w7 83pistXcESe9OypYytVwhLCZiv2ZYktoqFGM562R1fuJlgk8ky4BaxhAFUTqp/ukrHoh dnLSik83zbVC15FSsnD+EQdNLBA3SHjX6d1TEePmCrMAga8hitpBSblfhcIsrLzSpmOs 9gBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786059413; x=1786664213; h=content-type: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:content-type; bh=gK3sjM001OyHimEt3r9e3MTxT5BkhagjBxMzBV7doNU=; b=tCMICr0OF9F5BJ16RK0/aVQavm+PQlpKdxh/Wwcp1SvH1qntkKLN+Fj54jKTybWKTH W/XWWF2UgjVxxFWgde/Pnyf63q4F+X3VlrmcUNqwG5pvpQjFitIyvrux+4LMn5FoO0U1 vn7mOmkPU3t0m0T2kgyuYYK1kVvhT50R5n0sWFQBQXZroatfr7kkwXuhjLP2tsKE+K95 gDuRfv8jzAuBjCncqcV+MlEcS3j0Hg/KBoKji30jfcZg2NhAPEEK32Vhopbtcr6TIQjM Sb6+WUdx5L5hHba4NtziecSTsOpYkxa0aVk8ytpD79074EcaNeWeWwfJiaoUxQgcN+M5 4aLg== X-Forwarded-Encrypted: i=1; AHgh+RquHOxZR9SwjQglUWfKLDV+/CeFI3PQ8DmNKEyWULehD+bSHZ6A4BscLB7cA9d8sdKqXng=@vger.kernel.org X-Gm-Message-State: AOJu0YxK+v+jCOcPC7KQVAzh1obm+S7HscDUP939JLcZOq/BMGAw5+H2 veBfN5GT8FB7JM/DX0ZBHJd+5AqkTUM4gGWgrlwMg/lbXKEhKS0Wy/GIAkDTic6YdBnkBxde4NF PgbDN3A== X-Received: from pglu1.prod.google.com ([2002:a63:1401:0:b0:c99:aff5:7078]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6da8:b0:3c3:bee9:8eec with SMTP id adf61e73a8af0-3cbadc10910mr5429156637.19.1786059412806; Thu, 06 Aug 2026 16:36:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Aug 2026 16:35:40 -0700 In-Reply-To: <20260806233609.212337-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260806233609.212337-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260806233609.212337-24-seanjc@google.com> Subject: [PATCH v6 23/51] x86/tsc: Add standalone helper for getting CPU frequency from CPUID From: Sean Christopherson To: Kiryl Shutsemau , Rick Edgecombe , Sean Christopherson , Paolo Bonzini , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , Thomas Gleixner , John Stultz Cc: Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , Miroslav Lichvar , x86@kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, Michael Kelley , Tom Lendacky , Nikunj A Dadhania , David Woodhouse , David Woodhouse , 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 | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h index c09ec485abcd..cb682f097ea7 100644 --- a/arch/x86/include/asm/tsc.h +++ b/arch/x86/include/asm/tsc.h @@ -88,6 +88,7 @@ struct cpuid_tsc_info { unsigned int crystal_khz; }; extern int cpuid_get_tsc_info(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 d95e4f059f17..4c5328d19ebe 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -668,6 +668,18 @@ int cpuid_get_tsc_info(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. @@ -703,12 +715,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; @@ -733,19 +741,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.55.0.679.g6767b8d81c-goog