blob: 1465aaee543a78b0f6d34fae347ca9bbf2c68f52 [file] [log] [blame]
Bin Gao7da7c152013-10-21 09:16:33 -07001/*
Len Brown9e0cae92016-06-17 01:22:46 -04002 * tsc_msr.c - TSC frequency enumeration via MSR
Bin Gao7da7c152013-10-21 09:16:33 -07003 *
4 * Copyright (C) 2013 Intel Corporation
5 * Author: Bin Gao <bin.gao@intel.com>
6 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/kernel.h>
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030011
Bin Gao7da7c152013-10-21 09:16:33 -070012#include <asm/apic.h>
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030013#include <asm/cpu_device_id.h>
14#include <asm/intel-family.h>
15#include <asm/msr.h>
Bin Gao7da7c152013-10-21 09:16:33 -070016#include <asm/param.h>
Andy Shevchenkodbd0fbc2018-06-29 22:31:10 +030017#include <asm/tsc.h>
Bin Gao7da7c152013-10-21 09:16:33 -070018
Len Brown6fcb41c2016-06-17 01:22:48 -040019#define MAX_NUM_FREQS 9
Bin Gao7da7c152013-10-21 09:16:33 -070020
21/*
Len Brown9e0cae92016-06-17 01:22:46 -040022 * If MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
Bin Gao7da7c152013-10-21 09:16:33 -070023 * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
24 * Unfortunately some Intel Atom SoCs aren't quite compliant to this,
25 * so we need manually differentiate SoC families. This is what the
26 * field msr_plat does.
27 */
28struct freq_desc {
Bin Gao7da7c152013-10-21 09:16:33 -070029 u8 msr_plat; /* 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
30 u32 freqs[MAX_NUM_FREQS];
31};
32
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030033static const struct freq_desc freq_desc_pnw = {
34 0, { 0, 0, 0, 0, 0, 99840, 0, 83200 }
Bin Gao7da7c152013-10-21 09:16:33 -070035};
36
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030037static const struct freq_desc freq_desc_clv = {
38 0, { 0, 133200, 0, 0, 0, 99840, 0, 83200 }
39};
Bin Gao7da7c152013-10-21 09:16:33 -070040
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030041static const struct freq_desc freq_desc_byt = {
42 1, { 83300, 100000, 133300, 116700, 80000, 0, 0, 0 }
43};
Bin Gao7da7c152013-10-21 09:16:33 -070044
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030045static const struct freq_desc freq_desc_cht = {
46 1, { 83300, 100000, 133300, 116700, 80000, 93300, 90000, 88900, 87500 }
47};
Bin Gao7da7c152013-10-21 09:16:33 -070048
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030049static const struct freq_desc freq_desc_tng = {
50 1, { 0, 100000, 133300, 0, 0, 0, 0, 0 }
51};
52
53static const struct freq_desc freq_desc_ann = {
54 1, { 83300, 100000, 133300, 100000, 0, 0, 0, 0 }
55};
56
57static const struct x86_cpu_id tsc_msr_cpu_ids[] = {
58 INTEL_CPU_FAM6(ATOM_PENWELL, freq_desc_pnw),
59 INTEL_CPU_FAM6(ATOM_CLOVERVIEW, freq_desc_clv),
60 INTEL_CPU_FAM6(ATOM_SILVERMONT1, freq_desc_byt),
61 INTEL_CPU_FAM6(ATOM_AIRMONT, freq_desc_cht),
62 INTEL_CPU_FAM6(ATOM_MERRIFIELD, freq_desc_tng),
63 INTEL_CPU_FAM6(ATOM_MOOREFIELD, freq_desc_ann),
64 {}
65};
Bin Gao7da7c152013-10-21 09:16:33 -070066
67/*
Len Brown14bb4e32016-06-17 01:22:45 -040068 * MSR-based CPU/TSC frequency discovery for certain CPUs.
Thomas Gleixner5f0e0302014-02-19 13:52:29 +020069 *
Len Brown14bb4e32016-06-17 01:22:45 -040070 * Set global "lapic_timer_frequency" to bus_clock_cycles/jiffy
71 * Return processor base frequency in KHz, or 0 on failure.
Bin Gao7da7c152013-10-21 09:16:33 -070072 */
Len Brown02c0cd22016-06-17 01:22:50 -040073unsigned long cpu_khz_from_msr(void)
Bin Gao7da7c152013-10-21 09:16:33 -070074{
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030075 u32 lo, hi, ratio, freq;
76 const struct freq_desc *freq_desc;
77 const struct x86_cpu_id *id;
Thomas Gleixner5f0e0302014-02-19 13:52:29 +020078 unsigned long res;
Bin Gao7da7c152013-10-21 09:16:33 -070079
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030080 id = x86_match_cpu(tsc_msr_cpu_ids);
81 if (!id)
Len Brownba826832016-06-17 01:22:44 -040082 return 0;
83
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030084 freq_desc = (struct freq_desc *)id->driver_data;
85 if (freq_desc->msr_plat) {
Bin Gao7da7c152013-10-21 09:16:33 -070086 rdmsr(MSR_PLATFORM_INFO, lo, hi);
Chen Yu886123f2016-05-06 11:33:39 +080087 ratio = (lo >> 8) & 0xff;
Bin Gao7da7c152013-10-21 09:16:33 -070088 } else {
89 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
90 ratio = (hi >> 8) & 0x1f;
91 }
Bin Gao7da7c152013-10-21 09:16:33 -070092
93 /* Get FSB FREQ ID */
94 rdmsr(MSR_FSB_FREQ, lo, hi);
Andy Shevchenko397d3ad2018-06-29 22:31:09 +030095
96 /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
97 freq = freq_desc->freqs[lo & 0x7];
Bin Gao7da7c152013-10-21 09:16:33 -070098
99 /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
Thomas Gleixner5f0e0302014-02-19 13:52:29 +0200100 res = freq * ratio;
Bin Gao7da7c152013-10-21 09:16:33 -0700101
H. Peter Anvinca1e6312014-01-16 13:00:21 -0800102#ifdef CONFIG_X86_LOCAL_APIC
Bin Gao7da7c152013-10-21 09:16:33 -0700103 lapic_timer_frequency = (freq * 1000) / HZ;
H. Peter Anvinca1e6312014-01-16 13:00:21 -0800104#endif
Bin Gaof3a02ec2016-11-15 12:27:24 -0800105
106 /*
107 * TSC frequency determined by MSR is always considered "known"
108 * because it is reported by HW.
109 * Another fact is that on MSR capable platforms, PIT/HPET is
110 * generally not available so calibration won't work at all.
111 */
112 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
113
114 /*
115 * Unfortunately there is no way for hardware to tell whether the
116 * TSC is reliable. We were told by silicon design team that TSC
117 * on Atom SoCs are always "reliable". TSC is also the only
118 * reliable clocksource on these SoCs (HPET is either not present
119 * or not functional) so mark TSC reliable which removes the
120 * requirement for a watchdog clocksource.
121 */
122 setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
123
Thomas Gleixner5f0e0302014-02-19 13:52:29 +0200124 return res;
Bin Gao7da7c152013-10-21 09:16:33 -0700125}