cpupowerutils - cpufrequtils extended with quite some features
[linux-2.6.git] / tools / power / cpupower / utils / idle_monitor / snb_idle.c
1 /*
2  *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  *  Based on Len Brown's <lenb@kernel.org> turbostat tool.
7  */
8
9 #if defined(__i386__) || defined(__x86_64__)
10
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "helpers/helpers.h"
17 #include "idle_monitor/cpupower-monitor.h"
18
19 #define MSR_PKG_C2_RESIDENCY    0x60D
20 #define MSR_PKG_C7_RESIDENCY    0x3FA
21 #define MSR_CORE_C7_RESIDENCY   0x3FE
22
23 #define MSR_TSC 0x10
24
25 enum intel_snb_id { C7 = 0, PC2, PC7, SNB_CSTATE_COUNT, TSC = 0xFFFF };
26
27 static int snb_get_count_percent(unsigned int self_id, double *percent,
28                                  unsigned int cpu);
29
30 static cstate_t snb_cstates[SNB_CSTATE_COUNT] = {
31         {
32                 .name                   = "C7",
33                 .desc                   = N_("Processor Core C7"),
34                 .id                     = C7,
35                 .range                  = RANGE_CORE,
36                 .get_count_percent      = snb_get_count_percent,
37         },
38         {
39                 .name                   = "PC2",
40                 .desc                   = N_("Processor Package C2"),
41                 .id                     = PC2,
42                 .range                  = RANGE_PACKAGE,
43                 .get_count_percent      = snb_get_count_percent,
44         },
45         {
46                 .name                   = "PC7",
47                 .desc                   = N_("Processor Package C7"),
48                 .id                     = PC7,
49                 .range                  = RANGE_PACKAGE,
50                 .get_count_percent      = snb_get_count_percent,
51         },
52 };
53
54 static unsigned long long tsc_at_measure_start;
55 static unsigned long long tsc_at_measure_end;
56 static unsigned long long *previous_count[SNB_CSTATE_COUNT];
57 static unsigned long long *current_count[SNB_CSTATE_COUNT];
58 /* valid flag for all CPUs. If a MSR read failed it will be zero */
59 static int *is_valid;
60
61 static int snb_get_count(enum intel_snb_id id, unsigned long long *val, unsigned int cpu)
62 {
63         int msr;
64
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;
80         };
81         if (read_msr(cpu, msr, val))
82                 return -1;
83         return 0;
84 }
85
86 static int snb_get_count_percent(unsigned int id, double *percent,
87                                  unsigned int cpu)
88 {
89         *percent = 0.0;
90
91         if (!is_valid[cpu])
92                 return -1;
93
94         *percent = (100.0 * (current_count[id][cpu] - previous_count[id][cpu])) /
95                 (tsc_at_measure_end - tsc_at_measure_start);
96
97         dprint("%s: previous: %llu - current: %llu - (%u)\n", snb_cstates[id].name,
98                previous_count[id][cpu], current_count[id][cpu],
99                cpu);
100
101         dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
102                snb_cstates[id].name,
103                (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
104                current_count[id][cpu]
105                - previous_count[id][cpu],
106                *percent, cpu);
107
108         return 0;
109 }
110
111 static int snb_start(void)
112 {
113         int num, cpu;
114         unsigned long long val;
115
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;
120                 }
121         }
122         snb_get_count(TSC, &tsc_at_measure_start, 0);
123         return 0;
124 }
125
126 static int snb_stop(void)
127 {
128         unsigned long long val;
129         int num, cpu;
130
131         snb_get_count(TSC, &tsc_at_measure_end, 0);
132
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;
137                 }
138         }
139         return 0;
140 }
141
142 struct cpuidle_monitor intel_snb_monitor;
143
144 static struct cpuidle_monitor* snb_register(void) {
145
146         int num;
147
148         if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL
149             || cpupower_cpu_info.family != 6)
150                 return NULL;
151
152         if (cpupower_cpu_info.model != 0x2A
153             && cpupower_cpu_info.model != 0x2D)
154                 return NULL;
155
156         is_valid = calloc(cpu_count, sizeof (int));
157         for (num = 0; num < SNB_CSTATE_COUNT; num++) {
158                 previous_count[num] = calloc (cpu_count,
159                                               sizeof(unsigned long long));
160                 current_count[num]  = calloc (cpu_count,
161                                               sizeof(unsigned long long));
162         }
163         intel_snb_monitor.name_len = strlen(intel_snb_monitor.name);
164         return &intel_snb_monitor;
165 }
166
167 void snb_unregister(void)
168 {
169         int num;
170         free(is_valid);
171         for (num = 0; num < SNB_CSTATE_COUNT; num++) {
172                 free(previous_count[num]);
173                 free(current_count[num]);
174         }
175 }
176
177 struct cpuidle_monitor intel_snb_monitor = {
178         .name                   = "SandyBridge",
179         .hw_states              = snb_cstates,
180         .hw_states_num          = SNB_CSTATE_COUNT,
181         .start                  = snb_start,
182         .stop                   = snb_stop,
183         .do_register            = snb_register,
184         .unregister             = snb_unregister,
185         .needs_root             = 1,
186         .overflow_s             = 922000000 /* 922337203 seconds TSC overflow
187                                                at 20GHz */
188 };
189 #endif /* defined(__i386__) || defined(__x86_64__) */