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_C3_RESIDENCY 0x3F8
19 #define MSR_PKG_C6_RESIDENCY 0x3F9
20 #define MSR_CORE_C3_RESIDENCY 0x3FC
21 #define MSR_CORE_C6_RESIDENCY 0x3FD
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
,
32 static cstate_t nhm_cstates
[NHM_CSTATE_COUNT
] = {
35 .desc
= N_("Processor Core C3"),
38 .get_count_percent
= nhm_get_count_percent
,
42 .desc
= N_("Processor Core C6"),
45 .get_count_percent
= nhm_get_count_percent
,
50 .desc
= N_("Processor Package C3"),
52 .range
= RANGE_PACKAGE
,
53 .get_count_percent
= nhm_get_count_percent
,
57 .desc
= N_("Processor Package C6"),
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 */
71 static int nhm_get_count(enum intel_nhm_id id
, unsigned long long *val
,
78 msr
= MSR_CORE_C3_RESIDENCY
;
81 msr
= MSR_CORE_C6_RESIDENCY
;
84 msr
= MSR_PKG_C3_RESIDENCY
;
87 msr
= MSR_PKG_C6_RESIDENCY
;
95 if (read_msr(cpu
, msr
, val
))
101 static int nhm_get_count_percent(unsigned int id
, double *percent
,
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
],
126 static int nhm_start(void)
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
);
144 static int nhm_stop(void)
146 unsigned long long val
;
147 unsigned long long dbg
;
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
);
164 struct cpuidle_monitor intel_nhm_monitor
;
166 struct cpuidle_monitor
*intel_nhm_register(void)
170 if (cpupower_cpu_info
.vendor
!= X86_VENDOR_INTEL
)
173 if (!(cpupower_cpu_info
.caps
& CPUPOWER_CAP_INV_TSC
))
176 if (!(cpupower_cpu_info
.caps
& CPUPOWER_CAP_APERF
))
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)
196 for (num
= 0; num
< NHM_CSTATE_COUNT
; num
++) {
197 free(previous_count
[num
]);
198 free(current_count
[num
]);
203 struct cpuidle_monitor intel_nhm_monitor
= {
205 .hw_states_num
= NHM_CSTATE_COUNT
,
206 .hw_states
= nhm_cstates
,
209 .do_register
= intel_nhm_register
,
210 .unregister
= intel_nhm_unregister
,
211 .flags
.needs_root
= 1,
212 .overflow_s
= 922000000 /* 922337203 seconds TSC overflow