Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[linux-btrfs-devel.git] / tools / power / cpupower / utils / idle_monitor / nhm_idle.c
blobd2a91dd0d5637d70bfdba93ddf7d92ea84e4dc51
1 /*
2 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4 * Licensed under the terms of the GNU GPL License version 2.
6 * Based on Len Brown's <lenb@kernel.org> turbostat tool.
7 */
9 #if defined(__i386__) || defined(__x86_64__)
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #include "helpers/helpers.h"
17 #include "idle_monitor/cpupower-monitor.h"
19 #define MSR_PKG_C3_RESIDENCY 0x3F8
20 #define MSR_PKG_C6_RESIDENCY 0x3F9
21 #define MSR_CORE_C3_RESIDENCY 0x3FC
22 #define MSR_CORE_C6_RESIDENCY 0x3FD
24 #define MSR_TSC 0x10
26 #define NHM_CSTATE_COUNT 4
28 enum intel_nhm_id { C3 = 0, C6, PC3, PC6, TSC = 0xFFFF };
30 static int nhm_get_count_percent(unsigned int self_id, double *percent,
31 unsigned int cpu);
33 static cstate_t nhm_cstates[NHM_CSTATE_COUNT] = {
35 .name = "C3",
36 .desc = N_("Processor Core C3"),
37 .id = C3,
38 .range = RANGE_CORE,
39 .get_count_percent = nhm_get_count_percent,
42 .name = "C6",
43 .desc = N_("Processor Core C6"),
44 .id = C6,
45 .range = RANGE_CORE,
46 .get_count_percent = nhm_get_count_percent,
50 .name = "PC3",
51 .desc = N_("Processor Package C3"),
52 .id = PC3,
53 .range = RANGE_PACKAGE,
54 .get_count_percent = nhm_get_count_percent,
57 .name = "PC6",
58 .desc = N_("Processor Package C6"),
59 .id = PC6,
60 .range = RANGE_PACKAGE,
61 .get_count_percent = nhm_get_count_percent,
65 static unsigned long long tsc_at_measure_start;
66 static unsigned long long tsc_at_measure_end;
67 static unsigned long long *previous_count[NHM_CSTATE_COUNT];
68 static unsigned long long *current_count[NHM_CSTATE_COUNT];
69 /* valid flag for all CPUs. If a MSR read failed it will be zero */
70 static int *is_valid;
72 static int nhm_get_count(enum intel_nhm_id id, unsigned long long *val,
73 unsigned int cpu)
75 int msr;
77 switch (id) {
78 case C3:
79 msr = MSR_CORE_C3_RESIDENCY;
80 break;
81 case C6:
82 msr = MSR_CORE_C6_RESIDENCY;
83 break;
84 case PC3:
85 msr = MSR_PKG_C3_RESIDENCY;
86 break;
87 case PC6:
88 msr = MSR_PKG_C6_RESIDENCY;
89 break;
90 case TSC:
91 msr = MSR_TSC;
92 break;
93 default:
94 return -1;
96 if (read_msr(cpu, msr, val))
97 return -1;
99 return 0;
102 static int nhm_get_count_percent(unsigned int id, double *percent,
103 unsigned int cpu)
105 *percent = 0.0;
107 if (!is_valid[cpu])
108 return -1;
110 *percent = (100.0 *
111 (current_count[id][cpu] - previous_count[id][cpu])) /
112 (tsc_at_measure_end - tsc_at_measure_start);
114 dprint("%s: previous: %llu - current: %llu - (%u)\n",
115 nhm_cstates[id].name, previous_count[id][cpu],
116 current_count[id][cpu], cpu);
118 dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
119 nhm_cstates[id].name,
120 (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
121 current_count[id][cpu] - previous_count[id][cpu],
122 *percent, cpu);
124 return 0;
127 static int nhm_start(void)
129 int num, cpu;
130 unsigned long long dbg, val;
132 nhm_get_count(TSC, &tsc_at_measure_start, 0);
134 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
135 for (cpu = 0; cpu < cpu_count; cpu++) {
136 is_valid[cpu] = !nhm_get_count(num, &val, cpu);
137 previous_count[num][cpu] = val;
140 nhm_get_count(TSC, &dbg, 0);
141 dprint("TSC diff: %llu\n", dbg - tsc_at_measure_start);
142 return 0;
145 static int nhm_stop(void)
147 unsigned long long val;
148 unsigned long long dbg;
149 int num, cpu;
151 nhm_get_count(TSC, &tsc_at_measure_end, 0);
153 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
154 for (cpu = 0; cpu < cpu_count; cpu++) {
155 is_valid[cpu] = !nhm_get_count(num, &val, cpu);
156 current_count[num][cpu] = val;
159 nhm_get_count(TSC, &dbg, 0);
160 dprint("TSC diff: %llu\n", dbg - tsc_at_measure_end);
162 return 0;
165 struct cpuidle_monitor intel_nhm_monitor;
167 struct cpuidle_monitor *intel_nhm_register(void)
169 int num;
171 if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
172 return NULL;
174 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC))
175 return NULL;
177 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF))
178 return NULL;
180 /* Free this at program termination */
181 is_valid = calloc(cpu_count, sizeof(int));
182 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
183 previous_count[num] = calloc(cpu_count,
184 sizeof(unsigned long long));
185 current_count[num] = calloc(cpu_count,
186 sizeof(unsigned long long));
189 intel_nhm_monitor.name_len = strlen(intel_nhm_monitor.name);
190 return &intel_nhm_monitor;
193 void intel_nhm_unregister(void)
195 int num;
197 for (num = 0; num < NHM_CSTATE_COUNT; num++) {
198 free(previous_count[num]);
199 free(current_count[num]);
201 free(is_valid);
204 struct cpuidle_monitor intel_nhm_monitor = {
205 .name = "Nehalem",
206 .hw_states_num = NHM_CSTATE_COUNT,
207 .hw_states = nhm_cstates,
208 .start = nhm_start,
209 .stop = nhm_stop,
210 .do_register = intel_nhm_register,
211 .unregister = intel_nhm_unregister,
212 .needs_root = 1,
213 .overflow_s = 922000000 /* 922337203 seconds TSC overflow
214 at 20GHz */
216 #endif