1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
5 * Based on Len Brown's <lenb@kernel.org> turbostat tool.
8 #if defined(__i386__) || defined(__x86_64__)
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
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
,
29 static cstate_t snb_cstates
[SNB_CSTATE_COUNT
] = {
32 .desc
= N_("Processor Core C7"),
35 .get_count_percent
= snb_get_count_percent
,
39 .desc
= N_("Processor Package C2"),
41 .range
= RANGE_PACKAGE
,
42 .get_count_percent
= snb_get_count_percent
,
46 .desc
= N_("Processor Package C7"),
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 */
60 static int snb_get_count(enum intel_snb_id id
, unsigned long long *val
,
67 msr
= MSR_CORE_C7_RESIDENCY
;
70 msr
= MSR_PKG_C2_RESIDENCY
;
73 msr
= MSR_PKG_C7_RESIDENCY
;
81 if (read_msr(cpu
, msr
, val
))
86 static int snb_get_count_percent(unsigned int id
, double *percent
,
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
],
111 static int snb_start(void)
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
);
126 static int snb_stop(void)
128 unsigned long long val
;
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
;
142 struct cpuidle_monitor intel_snb_monitor
;
144 static struct cpuidle_monitor
*snb_register(void)
148 if (cpupower_cpu_info
.vendor
!= X86_VENDOR_INTEL
149 || cpupower_cpu_info
.family
!= 6)
152 switch (cpupower_cpu_info
.model
) {
154 case 0x2D: /* SNB Xeon */
156 case 0x3E: /* IVB Xeon */
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)
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
,
193 .do_register
= snb_register
,
194 .unregister
= snb_unregister
,
195 .flags
.needs_root
= 1,
196 .overflow_s
= 922000000 /* 922337203 seconds TSC overflow
199 #endif /* defined(__i386__) || defined(__x86_64__) */