aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDirk Brandewie <dirk.brandewie@gmail.com>2013-02-06 12:02:13 -0500
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2013-02-09 06:56:04 -0500
commit93f0822dff5dae2f0a2645f16300c14af41ca777 (patch)
treee991b2b798f4d218cc410e13701d687d8f7cba49 /drivers
parent633d47d653f3a717fb68293d24ab7059c480f71b (diff)
cpufreq/x86: Add P-state driver for sandy bridge.
Add a P-state driver for the Intel Sandy bridge processor. In cpufreq terminology this driver implements a scaling driver with an internal governor. When built into the the kernel this driver will be the preferred scaling driver for Sandy bridge processors. In addition to the interfaces provided by the cpufreq subsystem for controlling scaling drivers. The user may control the behavior of the driver via three sysfs files located in "/sys/devices/system/cpu/intel_pstate". max_perf_pct: limits the maximum P state that will be requested by the driver stated as a percentage of the avail performance. min_perf_pct: limits the minimum P state that will be requested by the driver stated as a percentage of the avail performance. no_turbo: limits the driver to selecting P states below the turbo frequency range. Signed-off-by: Dirk Brandewie <dirk.j.brandewie@intel.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/cpufreq/Kconfig.x8618
-rw-r--r--drivers/cpufreq/Makefile1
-rw-r--r--drivers/cpufreq/intel_pstate.c806
3 files changed, 825 insertions, 0 deletions
diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
index 7227cd734042..6aa7053ce2ef 100644
--- a/drivers/cpufreq/Kconfig.x86
+++ b/drivers/cpufreq/Kconfig.x86
@@ -2,6 +2,24 @@
2# x86 CPU Frequency scaling drivers 2# x86 CPU Frequency scaling drivers
3# 3#
4 4
5config X86_INTEL_PSTATE
6 tristate "Intel P state control"
7 depends on X86
8 help
9 This driver provides a P state for Intel core processors.
10 The driver implements an internal governor and will become
11 the scaling driver and governor for Sandy bridge processors.
12
13 When this driver is enabled it will become the perferred
14 scaling driver for Sandy bridge processors.
15
16 Note: This driver should be built with the same settings as
17 the other scaling drivers configured into the system
18 (module/built-in) in order for the driver to register itself
19 as the scaling driver on the system.
20
21 If in doubt, say N.
22
5config X86_PCC_CPUFREQ 23config X86_PCC_CPUFREQ
6 tristate "Processor Clocking Control interface driver" 24 tristate "Processor Clocking Control interface driver"
7 depends on ACPI && ACPI_PROCESSOR 25 depends on ACPI && ACPI_PROCESSOR
diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
index 93610b284b35..cd3d4ffe9626 100644
--- a/drivers/cpufreq/Makefile
+++ b/drivers/cpufreq/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_X86_SPEEDSTEP_SMI) += speedstep-smi.o
40obj-$(CONFIG_X86_SPEEDSTEP_CENTRINO) += speedstep-centrino.o 40obj-$(CONFIG_X86_SPEEDSTEP_CENTRINO) += speedstep-centrino.o
41obj-$(CONFIG_X86_P4_CLOCKMOD) += p4-clockmod.o 41obj-$(CONFIG_X86_P4_CLOCKMOD) += p4-clockmod.o
42obj-$(CONFIG_X86_CPUFREQ_NFORCE2) += cpufreq-nforce2.o 42obj-$(CONFIG_X86_CPUFREQ_NFORCE2) += cpufreq-nforce2.o
43obj-$(CONFIG_X86_INTEL_PSTATE) += intel_pstate.o
43 44
44################################################################################## 45##################################################################################
45# ARM SoC drivers 46# ARM SoC drivers
diff --git a/drivers/cpufreq/intel_pstate.c b/drivers/cpufreq/intel_pstate.c
new file mode 100644
index 000000000000..86ad4822305d
--- /dev/null
+++ b/drivers/cpufreq/intel_pstate.c
@@ -0,0 +1,806 @@
1/*
2 * cpufreq_snb.c: Native P state management for Intel processors
3 *
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 */
12
13#include <linux/kernel.h>
14#include <linux/kernel_stat.h>
15#include <linux/module.h>
16#include <linux/ktime.h>
17#include <linux/hrtimer.h>
18#include <linux/tick.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21#include <linux/list.h>
22#include <linux/cpu.h>
23#include <linux/cpufreq.h>
24#include <linux/sysfs.h>
25#include <linux/types.h>
26#include <linux/fs.h>
27#include <linux/debugfs.h>
28#include <trace/events/power.h>
29
30#include <asm/div64.h>
31#include <asm/msr.h>
32#include <asm/cpu_device_id.h>
33
34#define SAMPLE_COUNT 3
35
36#define FRAC_BITS 8
37#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
38#define fp_toint(X) ((X) >> FRAC_BITS)
39
40static inline int32_t mul_fp(int32_t x, int32_t y)
41{
42 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
43}
44
45static inline int32_t div_fp(int32_t x, int32_t y)
46{
47 return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
48}
49
50struct sample {
51 ktime_t start_time;
52 ktime_t end_time;
53 int core_pct_busy;
54 int pstate_pct_busy;
55 u64 duration_us;
56 u64 idletime_us;
57 u64 aperf;
58 u64 mperf;
59 int freq;
60};
61
62struct pstate_data {
63 int current_pstate;
64 int min_pstate;
65 int max_pstate;
66 int turbo_pstate;
67};
68
69struct _pid {
70 int setpoint;
71 int32_t integral;
72 int32_t p_gain;
73 int32_t i_gain;
74 int32_t d_gain;
75 int deadband;
76 int last_err;
77};
78
79struct cpudata {
80 int cpu;
81
82 char name[64];
83
84 struct timer_list timer;
85
86 struct pstate_adjust_policy *pstate_policy;
87 struct pstate_data pstate;
88 struct _pid pid;
89 struct _pid idle_pid;
90
91 int min_pstate_count;
92 int idle_mode;
93
94 ktime_t prev_sample;
95 u64 prev_idle_time_us;
96 u64 prev_aperf;
97 u64 prev_mperf;
98 int sample_ptr;
99 struct sample samples[SAMPLE_COUNT];
100};
101
102static struct cpudata **all_cpu_data;
103struct pstate_adjust_policy {
104 int sample_rate_ms;
105 int deadband;
106 int setpoint;
107 int p_gain_pct;
108 int d_gain_pct;
109 int i_gain_pct;
110};
111
112static struct pstate_adjust_policy default_policy = {
113 .sample_rate_ms = 10,
114 .deadband = 0,
115 .setpoint = 109,
116 .p_gain_pct = 17,
117 .d_gain_pct = 0,
118 .i_gain_pct = 4,
119};
120
121struct perf_limits {
122 int no_turbo;
123 int max_perf_pct;
124 int min_perf_pct;
125 int32_t max_perf;
126 int32_t min_perf;
127};
128
129static struct perf_limits limits = {
130 .no_turbo = 0,
131 .max_perf_pct = 100,
132 .max_perf = int_tofp(1),
133 .min_perf_pct = 0,
134 .min_perf = 0,
135};
136
137static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
138 int deadband, int integral) {
139 pid->setpoint = setpoint;
140 pid->deadband = deadband;
141 pid->integral = int_tofp(integral);
142 pid->last_err = setpoint - busy;
143}
144
145static inline void pid_p_gain_set(struct _pid *pid, int percent)
146{
147 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
148}
149
150static inline void pid_i_gain_set(struct _pid *pid, int percent)
151{
152 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
153}
154
155static inline void pid_d_gain_set(struct _pid *pid, int percent)
156{
157
158 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
159}
160
161static signed int pid_calc(struct _pid *pid, int busy)
162{
163 signed int err, result;
164 int32_t pterm, dterm, fp_error;
165 int32_t integral_limit;
166
167 err = pid->setpoint - busy;
168 fp_error = int_tofp(err);
169
170 if (abs(err) <= pid->deadband)
171 return 0;
172
173 pterm = mul_fp(pid->p_gain, fp_error);
174
175 pid->integral += fp_error;
176
177 /* limit the integral term */
178 integral_limit = int_tofp(30);
179 if (pid->integral > integral_limit)
180 pid->integral = integral_limit;
181 if (pid->integral < -integral_limit)
182 pid->integral = -integral_limit;
183
184 dterm = mul_fp(pid->d_gain, (err - pid->last_err));
185 pid->last_err = err;
186
187 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
188
189 return (signed int)fp_toint(result);
190}
191
192static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
193{
194 pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct);
195 pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct);
196 pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct);
197
198 pid_reset(&cpu->pid,
199 cpu->pstate_policy->setpoint,
200 100,
201 cpu->pstate_policy->deadband,
202 0);
203}
204
205static inline void intel_pstate_idle_pid_reset(struct cpudata *cpu)
206{
207 pid_p_gain_set(&cpu->idle_pid, cpu->pstate_policy->p_gain_pct);
208 pid_d_gain_set(&cpu->idle_pid, cpu->pstate_policy->d_gain_pct);
209 pid_i_gain_set(&cpu->idle_pid, cpu->pstate_policy->i_gain_pct);
210
211 pid_reset(&cpu->idle_pid,
212 75,
213 50,
214 cpu->pstate_policy->deadband,
215 0);
216}
217
218static inline void intel_pstate_reset_all_pid(void)
219{
220 unsigned int cpu;
221 for_each_online_cpu(cpu) {
222 if (all_cpu_data[cpu])
223 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
224 }
225}
226
227/************************** debugfs begin ************************/
228static int pid_param_set(void *data, u64 val)
229{
230 *(u32 *)data = val;
231 intel_pstate_reset_all_pid();
232 return 0;
233}
234static int pid_param_get(void *data, u64 *val)
235{
236 *val = *(u32 *)data;
237 return 0;
238}
239DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
240 pid_param_set, "%llu\n");
241
242struct pid_param {
243 char *name;
244 void *value;
245};
246
247static struct pid_param pid_files[] = {
248 {"sample_rate_ms", &default_policy.sample_rate_ms},
249 {"d_gain_pct", &default_policy.d_gain_pct},
250 {"i_gain_pct", &default_policy.i_gain_pct},
251 {"deadband", &default_policy.deadband},
252 {"setpoint", &default_policy.setpoint},
253 {"p_gain_pct", &default_policy.p_gain_pct},
254 {NULL, NULL}
255};
256
257static struct dentry *debugfs_parent;
258static void intel_pstate_debug_expose_params(void)
259{
260 int i = 0;
261
262 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
263 if (IS_ERR_OR_NULL(debugfs_parent))
264 return;
265 while (pid_files[i].name) {
266 debugfs_create_file(pid_files[i].name, 0660,
267 debugfs_parent, pid_files[i].value,
268 &fops_pid_param);
269 i++;
270 }
271}
272
273/************************** debugfs end ************************/
274
275/************************** sysfs begin ************************/
276#define show_one(file_name, object) \
277 static ssize_t show_##file_name \
278 (struct kobject *kobj, struct attribute *attr, char *buf) \
279 { \
280 return sprintf(buf, "%u\n", limits.object); \
281 }
282
283static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
284 const char *buf, size_t count)
285{
286 unsigned int input;
287 int ret;
288 ret = sscanf(buf, "%u", &input);
289 if (ret != 1)
290 return -EINVAL;
291 limits.no_turbo = clamp_t(int, input, 0 , 1);
292
293 return count;
294}
295
296static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
297 const char *buf, size_t count)
298{
299 unsigned int input;
300 int ret;
301 ret = sscanf(buf, "%u", &input);
302 if (ret != 1)
303 return -EINVAL;
304
305 limits.max_perf_pct = clamp_t(int, input, 0 , 100);
306 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
307 return count;
308}
309
310static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
311 const char *buf, size_t count)
312{
313 unsigned int input;
314 int ret;
315 ret = sscanf(buf, "%u", &input);
316 if (ret != 1)
317 return -EINVAL;
318 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
319 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
320
321 return count;
322}
323
324show_one(no_turbo, no_turbo);
325show_one(max_perf_pct, max_perf_pct);
326show_one(min_perf_pct, min_perf_pct);
327
328define_one_global_rw(no_turbo);
329define_one_global_rw(max_perf_pct);
330define_one_global_rw(min_perf_pct);
331
332static struct attribute *intel_pstate_attributes[] = {
333 &no_turbo.attr,
334 &max_perf_pct.attr,
335 &min_perf_pct.attr,
336 NULL
337};
338
339static struct attribute_group intel_pstate_attr_group = {
340 .attrs = intel_pstate_attributes,
341};
342static struct kobject *intel_pstate_kobject;
343
344static void intel_pstate_sysfs_expose_params(void)
345{
346 int rc;
347
348 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
349 &cpu_subsys.dev_root->kobj);
350 BUG_ON(!intel_pstate_kobject);
351 rc = sysfs_create_group(intel_pstate_kobject,
352 &intel_pstate_attr_group);
353 BUG_ON(rc);
354}
355
356/************************** sysfs end ************************/
357
358static int intel_pstate_min_pstate(void)
359{
360 u64 value;
361 rdmsrl(0xCE, value);
362 return (value >> 40) & 0xFF;
363}
364
365static int intel_pstate_max_pstate(void)
366{
367 u64 value;
368 rdmsrl(0xCE, value);
369 return (value >> 8) & 0xFF;
370}
371
372static int intel_pstate_turbo_pstate(void)
373{
374 u64 value;
375 int nont, ret;
376 rdmsrl(0x1AD, value);
377 nont = intel_pstate_max_pstate();
378 ret = ((value) & 255);
379 if (ret <= nont)
380 ret = nont;
381 return ret;
382}
383
384static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
385{
386 int max_perf = cpu->pstate.turbo_pstate;
387 int min_perf;
388 if (limits.no_turbo)
389 max_perf = cpu->pstate.max_pstate;
390
391 max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
392 *max = clamp_t(int, max_perf,
393 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
394
395 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
396 *min = clamp_t(int, min_perf,
397 cpu->pstate.min_pstate, max_perf);
398}
399
400static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
401{
402 int max_perf, min_perf;
403
404 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
405
406 pstate = clamp_t(int, pstate, min_perf, max_perf);
407
408 if (pstate == cpu->pstate.current_pstate)
409 return;
410
411#ifndef MODULE
412 trace_cpu_frequency(pstate * 100000, cpu->cpu);
413#endif
414 cpu->pstate.current_pstate = pstate;
415 wrmsrl(MSR_IA32_PERF_CTL, pstate << 8);
416
417}
418
419static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
420{
421 int target;
422 target = cpu->pstate.current_pstate + steps;
423
424 intel_pstate_set_pstate(cpu, target);
425}
426
427static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
428{
429 int target;
430 target = cpu->pstate.current_pstate - steps;
431 intel_pstate_set_pstate(cpu, target);
432}
433
434static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
435{
436 sprintf(cpu->name, "Intel 2nd generation core");
437
438 cpu->pstate.min_pstate = intel_pstate_min_pstate();
439 cpu->pstate.max_pstate = intel_pstate_max_pstate();
440 cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate();
441
442 /*
443 * goto max pstate so we don't slow up boot if we are built-in if we are
444 * a module we will take care of it during normal operation
445 */
446 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
447}
448
449static inline void intel_pstate_calc_busy(struct cpudata *cpu,
450 struct sample *sample)
451{
452 u64 core_pct;
453 sample->pstate_pct_busy = 100 - div64_u64(
454 sample->idletime_us * 100,
455 sample->duration_us);
456 core_pct = div64_u64(sample->aperf * 100, sample->mperf);
457 sample->freq = cpu->pstate.turbo_pstate * core_pct * 1000;
458
459 sample->core_pct_busy = sample->pstate_pct_busy * core_pct / 100;
460}
461
462static inline void intel_pstate_sample(struct cpudata *cpu)
463{
464 ktime_t now;
465 u64 idle_time_us;
466 u64 aperf, mperf;
467
468 now = ktime_get();
469 idle_time_us = get_cpu_idle_time_us(cpu->cpu, NULL);
470
471 rdmsrl(MSR_IA32_APERF, aperf);
472 rdmsrl(MSR_IA32_MPERF, mperf);
473 /* for the first sample, don't actually record a sample, just
474 * set the baseline */
475 if (cpu->prev_idle_time_us > 0) {
476 cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
477 cpu->samples[cpu->sample_ptr].start_time = cpu->prev_sample;
478 cpu->samples[cpu->sample_ptr].end_time = now;
479 cpu->samples[cpu->sample_ptr].duration_us =
480 ktime_us_delta(now, cpu->prev_sample);
481 cpu->samples[cpu->sample_ptr].idletime_us =
482 idle_time_us - cpu->prev_idle_time_us;
483
484 cpu->samples[cpu->sample_ptr].aperf = aperf;
485 cpu->samples[cpu->sample_ptr].mperf = mperf;
486 cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
487 cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
488
489 intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
490 }
491
492 cpu->prev_sample = now;
493 cpu->prev_idle_time_us = idle_time_us;
494 cpu->prev_aperf = aperf;
495 cpu->prev_mperf = mperf;
496}
497
498static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
499{
500 int sample_time, delay;
501
502 sample_time = cpu->pstate_policy->sample_rate_ms;
503 delay = msecs_to_jiffies(sample_time);
504 delay -= jiffies % delay;
505 mod_timer_pinned(&cpu->timer, jiffies + delay);
506}
507
508static inline void intel_pstate_idle_mode(struct cpudata *cpu)
509{
510 cpu->idle_mode = 1;
511}
512
513static inline void intel_pstate_normal_mode(struct cpudata *cpu)
514{
515 cpu->idle_mode = 0;
516}
517
518static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu)
519{
520 int32_t busy_scaled;
521 int32_t core_busy, turbo_pstate, current_pstate;
522
523 core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy);
524 turbo_pstate = int_tofp(cpu->pstate.turbo_pstate);
525 current_pstate = int_tofp(cpu->pstate.current_pstate);
526 busy_scaled = mul_fp(core_busy, div_fp(turbo_pstate, current_pstate));
527
528 return fp_toint(busy_scaled);
529}
530
531static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
532{
533 int busy_scaled;
534 struct _pid *pid;
535 signed int ctl = 0;
536 int steps;
537
538 pid = &cpu->pid;
539 busy_scaled = intel_pstate_get_scaled_busy(cpu);
540
541 ctl = pid_calc(pid, busy_scaled);
542
543 steps = abs(ctl);
544 if (ctl < 0)
545 intel_pstate_pstate_increase(cpu, steps);
546 else
547 intel_pstate_pstate_decrease(cpu, steps);
548}
549
550static inline void intel_pstate_adjust_idle_pstate(struct cpudata *cpu)
551{
552 int busy_scaled;
553 struct _pid *pid;
554 int ctl = 0;
555 int steps;
556
557 pid = &cpu->idle_pid;
558
559 busy_scaled = intel_pstate_get_scaled_busy(cpu);
560
561 ctl = pid_calc(pid, 100 - busy_scaled);
562
563 steps = abs(ctl);
564 if (ctl < 0)
565 intel_pstate_pstate_decrease(cpu, steps);
566 else
567 intel_pstate_pstate_increase(cpu, steps);
568
569 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate)
570 intel_pstate_normal_mode(cpu);
571}
572
573static void intel_pstate_timer_func(unsigned long __data)
574{
575 struct cpudata *cpu = (struct cpudata *) __data;
576
577 intel_pstate_sample(cpu);
578
579 if (!cpu->idle_mode)
580 intel_pstate_adjust_busy_pstate(cpu);
581 else
582 intel_pstate_adjust_idle_pstate(cpu);
583
584#if defined(XPERF_FIX)
585 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
586 cpu->min_pstate_count++;
587 if (!(cpu->min_pstate_count % 5)) {
588 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
589 intel_pstate_idle_mode(cpu);
590 }
591 } else
592 cpu->min_pstate_count = 0;
593#endif
594 intel_pstate_set_sample_time(cpu);
595}
596
597#define ICPU(model, policy) \
598 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy }
599
600static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
601 ICPU(0x2a, default_policy),
602 ICPU(0x2d, default_policy),
603 {}
604};
605MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
606
607static int intel_pstate_init_cpu(unsigned int cpunum)
608{
609
610 const struct x86_cpu_id *id;
611 struct cpudata *cpu;
612
613 id = x86_match_cpu(intel_pstate_cpu_ids);
614 if (!id)
615 return -ENODEV;
616
617 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
618 if (!all_cpu_data[cpunum])
619 return -ENOMEM;
620
621 cpu = all_cpu_data[cpunum];
622
623 intel_pstate_get_cpu_pstates(cpu);
624
625 cpu->cpu = cpunum;
626 cpu->pstate_policy =
627 (struct pstate_adjust_policy *)id->driver_data;
628 init_timer_deferrable(&cpu->timer);
629 cpu->timer.function = intel_pstate_timer_func;
630 cpu->timer.data =
631 (unsigned long)cpu;
632 cpu->timer.expires = jiffies + HZ/100;
633 intel_pstate_busy_pid_reset(cpu);
634 intel_pstate_idle_pid_reset(cpu);
635 intel_pstate_sample(cpu);
636 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
637
638 add_timer_on(&cpu->timer, cpunum);
639
640 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
641
642 return 0;
643}
644
645static unsigned int intel_pstate_get(unsigned int cpu_num)
646{
647 struct sample *sample;
648 struct cpudata *cpu;
649
650 cpu = all_cpu_data[cpu_num];
651 if (!cpu)
652 return 0;
653 sample = &cpu->samples[cpu->sample_ptr];
654 return sample->freq;
655}
656
657static int intel_pstate_set_policy(struct cpufreq_policy *policy)
658{
659 struct cpudata *cpu;
660 int min, max;
661
662 cpu = all_cpu_data[policy->cpu];
663
664 intel_pstate_get_min_max(cpu, &min, &max);
665
666 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
667 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
668 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
669
670 limits.max_perf_pct = policy->max * 100 / policy->cpuinfo.max_freq;
671 limits.max_perf_pct = clamp_t(int, limits.max_perf_pct, 0 , 100);
672 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
673
674 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
675 limits.min_perf_pct = 100;
676 limits.min_perf = int_tofp(1);
677 limits.max_perf_pct = 100;
678 limits.max_perf = int_tofp(1);
679 limits.no_turbo = 0;
680 }
681
682 return 0;
683}
684
685static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
686{
687 cpufreq_verify_within_limits(policy,
688 policy->cpuinfo.min_freq,
689 policy->cpuinfo.max_freq);
690
691 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
692 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
693 return -EINVAL;
694
695 return 0;
696}
697
698static int __cpuinit intel_pstate_cpu_exit(struct cpufreq_policy *policy)
699{
700 int cpu = policy->cpu;
701
702 del_timer(&all_cpu_data[cpu]->timer);
703 kfree(all_cpu_data[cpu]);
704 all_cpu_data[cpu] = NULL;
705 return 0;
706}
707
708static int __cpuinit intel_pstate_cpu_init(struct cpufreq_policy *policy)
709{
710 int rc, min_pstate, max_pstate;
711 struct cpudata *cpu;
712
713 rc = intel_pstate_init_cpu(policy->cpu);
714 if (rc)
715 return rc;
716
717 cpu = all_cpu_data[policy->cpu];
718
719 if (!limits.no_turbo &&
720 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
721 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
722 else
723 policy->policy = CPUFREQ_POLICY_POWERSAVE;
724
725 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
726 policy->min = min_pstate * 100000;
727 policy->max = max_pstate * 100000;
728
729 /* cpuinfo and default policy values */
730 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
731 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
732 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
733 cpumask_set_cpu(policy->cpu, policy->cpus);
734
735 return 0;
736}
737
738static struct cpufreq_driver intel_pstate_driver = {
739 .flags = CPUFREQ_CONST_LOOPS,
740 .verify = intel_pstate_verify_policy,
741 .setpolicy = intel_pstate_set_policy,
742 .get = intel_pstate_get,
743 .init = intel_pstate_cpu_init,
744 .exit = intel_pstate_cpu_exit,
745 .name = "intel_pstate",
746 .owner = THIS_MODULE,
747};
748
749static void intel_pstate_exit(void)
750{
751 int cpu;
752
753 sysfs_remove_group(intel_pstate_kobject,
754 &intel_pstate_attr_group);
755 debugfs_remove_recursive(debugfs_parent);
756
757 cpufreq_unregister_driver(&intel_pstate_driver);
758
759 if (!all_cpu_data)
760 return;
761
762 get_online_cpus();
763 for_each_online_cpu(cpu) {
764 if (all_cpu_data[cpu]) {
765 del_timer_sync(&all_cpu_data[cpu]->timer);
766 kfree(all_cpu_data[cpu]);
767 }
768 }
769
770 put_online_cpus();
771 vfree(all_cpu_data);
772}
773module_exit(intel_pstate_exit);
774
775static int __init intel_pstate_init(void)
776{
777 int rc = 0;
778 const struct x86_cpu_id *id;
779
780 id = x86_match_cpu(intel_pstate_cpu_ids);
781 if (!id)
782 return -ENODEV;
783
784 pr_info("Intel P-state driver initializing.\n");
785
786 all_cpu_data = vmalloc(sizeof(void *) * num_possible_cpus());
787 if (!all_cpu_data)
788 return -ENOMEM;
789 memset(all_cpu_data, 0, sizeof(void *) * num_possible_cpus());
790
791 rc = cpufreq_register_driver(&intel_pstate_driver);
792 if (rc)
793 goto out;
794
795 intel_pstate_debug_expose_params();
796 intel_pstate_sysfs_expose_params();
797 return rc;
798out:
799 intel_pstate_exit();
800 return -ENODEV;
801}
802device_initcall(intel_pstate_init);
803
804MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
805MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
806MODULE_LICENSE("GPL");