blob: 3d0e9aeea7c8ee51856ba4ff958859272d41eaa2 [file] [log] [blame]
Andy Shevchenko5067b082018-06-29 22:31:11 +03001// SPDX-License-Identifier: GPL-2.0
Bin Gao7da7c152013-10-21 09:16:33 -07002/*
Andy Shevchenko5067b082018-06-29 22:31:11 +03003 * TSC frequency enumeration via MSR
Bin Gao7da7c152013-10-21 09:16:33 -07004 *
Andy Shevchenko5067b082018-06-29 22:31:11 +03005 * Copyright (C) 2013, 2018 Intel Corporation
Bin Gao7da7c152013-10-21 09:16:33 -07006 * Author: Bin Gao <bin.gao@intel.com>
Bin Gao7da7c152013-10-21 09:16:33 -07007 */
8
9#include <linux/kernel.h>
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030010
Bin Gao7da7c152013-10-21 09:16:33 -070011#include <asm/apic.h>
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030012#include <asm/cpu_device_id.h>
13#include <asm/intel-family.h>
14#include <asm/msr.h>
Bin Gao7da7c152013-10-21 09:16:33 -070015#include <asm/param.h>
Andy Shevchenkodbd0fbc2018-06-29 22:31:10 +030016#include <asm/tsc.h>
Bin Gao7da7c152013-10-21 09:16:33 -070017
Len Brown6fcb41c2016-06-17 01:22:48 -040018#define MAX_NUM_FREQS 9
Bin Gao7da7c152013-10-21 09:16:33 -070019
20/*
Len Brown9e0cae92016-06-17 01:22:46 -040021 * If MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
Bin Gao7da7c152013-10-21 09:16:33 -070022 * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
23 * Unfortunately some Intel Atom SoCs aren't quite compliant to this,
24 * so we need manually differentiate SoC families. This is what the
25 * field msr_plat does.
26 */
27struct freq_desc {
Bin Gao7da7c152013-10-21 09:16:33 -070028 u8 msr_plat; /* 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
29 u32 freqs[MAX_NUM_FREQS];
30};
31
Andy Shevchenkod99e5da2018-06-29 22:31:12 +030032/*
33 * Penwell and Clovertrail use spread spectrum clock,
34 * so the freq number is not exactly the same as reported
35 * by MSR based on SDM.
36 */
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030037static const struct freq_desc freq_desc_pnw = {
38 0, { 0, 0, 0, 0, 0, 99840, 0, 83200 }
Bin Gao7da7c152013-10-21 09:16:33 -070039};
40
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030041static const struct freq_desc freq_desc_clv = {
42 0, { 0, 133200, 0, 0, 0, 99840, 0, 83200 }
43};
Bin Gao7da7c152013-10-21 09:16:33 -070044
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030045static const struct freq_desc freq_desc_byt = {
46 1, { 83300, 100000, 133300, 116700, 80000, 0, 0, 0 }
47};
Bin Gao7da7c152013-10-21 09:16:33 -070048
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030049static const struct freq_desc freq_desc_cht = {
50 1, { 83300, 100000, 133300, 116700, 80000, 93300, 90000, 88900, 87500 }
51};
Bin Gao7da7c152013-10-21 09:16:33 -070052
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030053static const struct freq_desc freq_desc_tng = {
54 1, { 0, 100000, 133300, 0, 0, 0, 0, 0 }
55};
56
57static const struct freq_desc freq_desc_ann = {
58 1, { 83300, 100000, 133300, 100000, 0, 0, 0, 0 }
59};
60
61static const struct x86_cpu_id tsc_msr_cpu_ids[] = {
Peter Zijlstraf2c4db12018-08-07 10:17:27 -070062 INTEL_CPU_FAM6(ATOM_SALTWELL_MID, freq_desc_pnw),
63 INTEL_CPU_FAM6(ATOM_SALTWELL_TABLET, freq_desc_clv),
64 INTEL_CPU_FAM6(ATOM_SILVERMONT, freq_desc_byt),
65 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, freq_desc_tng),
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030066 INTEL_CPU_FAM6(ATOM_AIRMONT, freq_desc_cht),
Peter Zijlstraf2c4db12018-08-07 10:17:27 -070067 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, freq_desc_ann),
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030068 {}
69};
Bin Gao7da7c152013-10-21 09:16:33 -070070
71/*
Len Brown14bb4e32016-06-17 01:22:45 -040072 * MSR-based CPU/TSC frequency discovery for certain CPUs.
Thomas Gleixner5f0e0302014-02-19 13:52:29 +020073 *
Len Brown14bb4e32016-06-17 01:22:45 -040074 * Set global "lapic_timer_frequency" to bus_clock_cycles/jiffy
75 * Return processor base frequency in KHz, or 0 on failure.
Bin Gao7da7c152013-10-21 09:16:33 -070076 */
Len Brown02c0cd22016-06-17 01:22:50 -040077unsigned long cpu_khz_from_msr(void)
Bin Gao7da7c152013-10-21 09:16:33 -070078{
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030079 u32 lo, hi, ratio, freq;
80 const struct freq_desc *freq_desc;
81 const struct x86_cpu_id *id;
Thomas Gleixner5f0e0302014-02-19 13:52:29 +020082 unsigned long res;
Bin Gao7da7c152013-10-21 09:16:33 -070083
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030084 id = x86_match_cpu(tsc_msr_cpu_ids);
85 if (!id)
Len Brownba826832016-06-17 01:22:44 -040086 return 0;
87
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030088 freq_desc = (struct freq_desc *)id->driver_data;
89 if (freq_desc->msr_plat) {
Bin Gao7da7c152013-10-21 09:16:33 -070090 rdmsr(MSR_PLATFORM_INFO, lo, hi);
Chen Yu886123f2016-05-06 11:33:39 +080091 ratio = (lo >> 8) & 0xff;
Bin Gao7da7c152013-10-21 09:16:33 -070092 } else {
93 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
94 ratio = (hi >> 8) & 0x1f;
95 }
Bin Gao7da7c152013-10-21 09:16:33 -070096
97 /* Get FSB FREQ ID */
98 rdmsr(MSR_FSB_FREQ, lo, hi);
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030099
100 /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
101 freq = freq_desc->freqs[lo & 0x7];
Bin Gao7da7c152013-10-21 09:16:33 -0700102
103 /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
Thomas Gleixner5f0e0302014-02-19 13:52:29 +0200104 res = freq * ratio;
Bin Gao7da7c152013-10-21 09:16:33 -0700105
H. Peter Anvinca1e6312014-01-16 13:00:21 -0800106#ifdef CONFIG_X86_LOCAL_APIC
Bin Gao7da7c152013-10-21 09:16:33 -0700107 lapic_timer_frequency = (freq * 1000) / HZ;
H. Peter Anvinca1e6312014-01-16 13:00:21 -0800108#endif
Bin Gaof3a02ec2016-11-15 12:27:24 -0800109
110 /*
111 * TSC frequency determined by MSR is always considered "known"
112 * because it is reported by HW.
113 * Another fact is that on MSR capable platforms, PIT/HPET is
114 * generally not available so calibration won't work at all.
115 */
116 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
117
118 /*
119 * Unfortunately there is no way for hardware to tell whether the
120 * TSC is reliable. We were told by silicon design team that TSC
121 * on Atom SoCs are always "reliable". TSC is also the only
122 * reliable clocksource on these SoCs (HPET is either not present
123 * or not functional) so mark TSC reliable which removes the
124 * requirement for a watchdog clocksource.
125 */
126 setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
127
Thomas Gleixner5f0e0302014-02-19 13:52:29 +0200128 return res;
Bin Gao7da7c152013-10-21 09:16:33 -0700129}