Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / power / cpupower / utils / idle_monitor / snb_idle.c
blob811d63ab17a769ef053cb288f8510d8ea161692d
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_C2_RESIDENCY 0x60D
19 #define MSR_PKG_C7_RESIDENCY 0x3FA
20 #define MSR_CORE_C7_RESIDENCY 0x3FE
22 #define MSR_TSC 0x10
24 enum intel_snb_id { C7 = 0, PC2, PC7, SNB_CSTATE_COUNT, TSC = 0xFFFF };
26 static int snb_get_count_percent(unsigned int self_id, double *percent,
27 unsigned int cpu);
29 static cstate_t snb_cstates[SNB_CSTATE_COUNT] = {
31 .name = "C7",
32 .desc = N_("Processor Core C7"),
33 .id = C7,
34 .range = RANGE_CORE,
35 .get_count_percent = snb_get_count_percent,
38 .name = "PC2",
39 .desc = N_("Processor Package C2"),
40 .id = PC2,
41 .range = RANGE_PACKAGE,
42 .get_count_percent = snb_get_count_percent,
45 .name = "PC7",
46 .desc = N_("Processor Package C7"),
47 .id = PC7,
48 .range = RANGE_PACKAGE,
49 .get_count_percent = snb_get_count_percent,
53 static unsigned long long tsc_at_measure_start;
54 static unsigned long long tsc_at_measure_end;
55 static unsigned long long *previous_count[SNB_CSTATE_COUNT];
56 static unsigned long long *current_count[SNB_CSTATE_COUNT];
57 /* valid flag for all CPUs. If a MSR read failed it will be zero */
58 static int *is_valid;
60 static int snb_get_count(enum intel_snb_id id, unsigned long long *val,
61 unsigned int cpu)
63 int msr;
65 switch (id) {
66 case C7:
67 msr = MSR_CORE_C7_RESIDENCY;
68 break;
69 case PC2:
70 msr = MSR_PKG_C2_RESIDENCY;
71 break;
72 case PC7:
73 msr = MSR_PKG_C7_RESIDENCY;
74 break;
75 case TSC:
76 msr = MSR_TSC;
77 break;
78 default:
79 return -1;
81 if (read_msr(cpu, msr, val))
82 return -1;
83 return 0;
86 static int snb_get_count_percent(unsigned int id, double *percent,
87 unsigned int cpu)
89 *percent = 0.0;
91 if (!is_valid[cpu])
92 return -1;
94 *percent = (100.0 *
95 (current_count[id][cpu] - previous_count[id][cpu])) /
96 (tsc_at_measure_end - tsc_at_measure_start);
98 dprint("%s: previous: %llu - current: %llu - (%u)\n",
99 snb_cstates[id].name, previous_count[id][cpu],
100 current_count[id][cpu], cpu);
102 dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
103 snb_cstates[id].name,
104 (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
105 current_count[id][cpu] - previous_count[id][cpu],
106 *percent, cpu);
108 return 0;
111 static int snb_start(void)
113 int num, cpu;
114 unsigned long long val;
116 for (num = 0; num < SNB_CSTATE_COUNT; num++) {
117 for (cpu = 0; cpu < cpu_count; cpu++) {
118 snb_get_count(num, &val, cpu);
119 previous_count[num][cpu] = val;
122 snb_get_count(TSC, &tsc_at_measure_start, base_cpu);
123 return 0;
126 static int snb_stop(void)
128 unsigned long long val;
129 int num, cpu;
131 snb_get_count(TSC, &tsc_at_measure_end, base_cpu);
133 for (num = 0; num < SNB_CSTATE_COUNT; num++) {
134 for (cpu = 0; cpu < cpu_count; cpu++) {
135 is_valid[cpu] = !snb_get_count(num, &val, cpu);
136 current_count[num][cpu] = val;
139 return 0;
142 struct cpuidle_monitor intel_snb_monitor;
144 static struct cpuidle_monitor *snb_register(void)
146 int num;
148 if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL
149 || cpupower_cpu_info.family != 6)
150 return NULL;
152 switch (cpupower_cpu_info.model) {
153 case 0x2A: /* SNB */
154 case 0x2D: /* SNB Xeon */
155 case 0x3A: /* IVB */
156 case 0x3E: /* IVB Xeon */
157 case 0x3C: /* HSW */
158 case 0x3F: /* HSW */
159 case 0x45: /* HSW */
160 case 0x46: /* HSW */
161 break;
162 default:
163 return NULL;
166 is_valid = calloc(cpu_count, sizeof(int));
167 for (num = 0; num < SNB_CSTATE_COUNT; num++) {
168 previous_count[num] = calloc(cpu_count,
169 sizeof(unsigned long long));
170 current_count[num] = calloc(cpu_count,
171 sizeof(unsigned long long));
173 intel_snb_monitor.name_len = strlen(intel_snb_monitor.name);
174 return &intel_snb_monitor;
177 void snb_unregister(void)
179 int num;
180 free(is_valid);
181 for (num = 0; num < SNB_CSTATE_COUNT; num++) {
182 free(previous_count[num]);
183 free(current_count[num]);
187 struct cpuidle_monitor intel_snb_monitor = {
188 .name = "SandyBridge",
189 .hw_states = snb_cstates,
190 .hw_states_num = SNB_CSTATE_COUNT,
191 .start = snb_start,
192 .stop = snb_stop,
193 .do_register = snb_register,
194 .unregister = snb_unregister,
195 .flags.needs_root = 1,
196 .overflow_s = 922000000 /* 922337203 seconds TSC overflow
197 at 20GHz */
199 #endif /* defined(__i386__) || defined(__x86_64__) */