Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / power / cpupower / utils / idle_monitor / nhm_idle.c
blob16eaf006f61f28b3dbd519ab923e5f0a059d385b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
5 * Based on Len Brown's <lenb@kernel.org> turbostat tool.
6 */
8 #if defined(__i386__) || defined(__x86_64__)
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include "helpers/helpers.h"
16 #include "idle_monitor/cpupower-monitor.h"
18 #define MSR_PKG_C3_RESIDENCY 0x3F8
19 #define MSR_PKG_C6_RESIDENCY 0x3F9
20 #define MSR_CORE_C3_RESIDENCY 0x3FC
21 #define MSR_CORE_C6_RESIDENCY 0x3FD
23 #define MSR_TSC 0x10
25 #define NHM_CSTATE_COUNT 4
27 enum intel_nhm_id { C3 = 0, C6, PC3, PC6, TSC = 0xFFFF };
29 static int nhm_get_count_percent(unsigned int self_id, double *percent,
30 unsigned int cpu);
32 static cstate_t nhm_cstates[NHM_CSTATE_COUNT] = {
34 .name = "C3",
35 .desc = N_("Processor Core C3"),
36 .id = C3,
37 .range = RANGE_CORE,
38 .get_count_percent = nhm_get_count_percent,
41 .name = "C6",
42 .desc = N_("Processor Core C6"),
43 .id = C6,
44 .range = RANGE_CORE,
45 .get_count_percent = nhm_get_count_percent,
49 .name = "PC3",
50 .desc = N_("Processor Package C3"),
51 .id = PC3,
52 .range = RANGE_PACKAGE,
53 .get_count_percent = nhm_get_count_percent,
56 .name = "PC6",
57 .desc = N_("Processor Package C6"),
58 .id = PC6,
59 .range = RANGE_PACKAGE,
60 .get_count_percent = nhm_get_count_percent,
64 static unsigned long long tsc_at_measure_start;
65 static unsigned long long tsc_at_measure_end;
66 static unsigned long long *previous_count[NHM_CSTATE_COUNT];
67 static unsigned long long *current_count[NHM_CSTATE_COUNT];
68 /* valid flag for all CPUs. If a MSR read failed it will be zero */
69 static int *is_valid;
71 static int nhm_get_count(enum intel_nhm_id id, unsigned long long *val,
72 unsigned int cpu)
74 int msr;
76 switch (id) {
77 case C3:
78 msr = MSR_CORE_C3_RESIDENCY;
79 break;
80 case C6:
81 msr = MSR_CORE_C6_RESIDENCY;
82 break;
83 case PC3:
84 msr = MSR_PKG_C3_RESIDENCY;
85 break;
86 case PC6:
87 msr = MSR_PKG_C6_RESIDENCY;
88 break;
89 case TSC:
90 msr = MSR_TSC;
91 break;
92 default:
93 return -1;
95 if (read_msr(cpu, msr, val))
96 return -1;
98 return 0;
101 static int nhm_get_count_percent(unsigned int id, double *percent,
102 unsigned int cpu)
104 *percent = 0.0;
106 if (!is_valid[cpu])
107 return -1;
109 *percent = (100.0 *
110 (current_count[id][cpu] - previous_count[id][cpu])) /
111 (tsc_at_measure_end - tsc_at_measure_start);
113 dprint("%s: previous: %llu - current: %llu - (%u)\n",
114 nhm_cstates[id].name, previous_count[id][cpu],
115 current_count[id][cpu], cpu);
117 dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
118 nhm_cstates[id].name,
119 (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
120 current_count[id][cpu] - previous_count[id][cpu],
121 *percent, cpu);
123 return 0;
126 static int nhm_start(void)
128 int num, cpu;
129 unsigned long long dbg, val;
131 nhm_get_count(TSC, &tsc_at_measure_start, base_cpu);
133 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
134 for (cpu = 0; cpu < cpu_count; cpu++) {
135 is_valid[cpu] = !nhm_get_count(num, &val, cpu);
136 previous_count[num][cpu] = val;
139 nhm_get_count(TSC, &dbg, base_cpu);
140 dprint("TSC diff: %llu\n", dbg - tsc_at_measure_start);
141 return 0;
144 static int nhm_stop(void)
146 unsigned long long val;
147 unsigned long long dbg;
148 int num, cpu;
150 nhm_get_count(TSC, &tsc_at_measure_end, base_cpu);
152 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
153 for (cpu = 0; cpu < cpu_count; cpu++) {
154 is_valid[cpu] = !nhm_get_count(num, &val, cpu);
155 current_count[num][cpu] = val;
158 nhm_get_count(TSC, &dbg, base_cpu);
159 dprint("TSC diff: %llu\n", dbg - tsc_at_measure_end);
161 return 0;
164 struct cpuidle_monitor intel_nhm_monitor;
166 struct cpuidle_monitor *intel_nhm_register(void)
168 int num;
170 if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
171 return NULL;
173 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC))
174 return NULL;
176 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF))
177 return NULL;
179 /* Free this at program termination */
180 is_valid = calloc(cpu_count, sizeof(int));
181 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
182 previous_count[num] = calloc(cpu_count,
183 sizeof(unsigned long long));
184 current_count[num] = calloc(cpu_count,
185 sizeof(unsigned long long));
188 intel_nhm_monitor.name_len = strlen(intel_nhm_monitor.name);
189 return &intel_nhm_monitor;
192 void intel_nhm_unregister(void)
194 int num;
196 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
197 free(previous_count[num]);
198 free(current_count[num]);
200 free(is_valid);
203 struct cpuidle_monitor intel_nhm_monitor = {
204 .name = "Nehalem",
205 .hw_states_num = NHM_CSTATE_COUNT,
206 .hw_states = nhm_cstates,
207 .start = nhm_start,
208 .stop = nhm_stop,
209 .do_register = intel_nhm_register,
210 .unregister = intel_nhm_unregister,
211 .flags.needs_root = 1,
212 .overflow_s = 922000000 /* 922337203 seconds TSC overflow
213 at 20GHz */
215 #endif