intel_pstate: add sample time scaling
[deliverable/linux.git] / drivers / cpufreq / intel_pstate.c
CommitLineData
93f0822d 1/*
d1b68485 2 * intel_pstate.c: Native P state management for Intel processors
93f0822d
DB
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>
fbbcdc07 28#include <linux/acpi.h>
93f0822d
DB
29#include <trace/events/power.h>
30
31#include <asm/div64.h>
32#include <asm/msr.h>
33#include <asm/cpu_device_id.h>
34
35#define SAMPLE_COUNT 3
36
61d8d2ab
DB
37#define BYT_RATIOS 0x66a
38#define BYT_VIDS 0x66b
39#define BYT_TURBO_RATIOS 0x66c
21855ff5 40#define BYT_TURBO_VIDS 0x66d
61d8d2ab 41
19e77c28 42
f0fe3cd7 43#define FRAC_BITS 8
93f0822d
DB
44#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
45#define fp_toint(X) ((X) >> FRAC_BITS)
f0fe3cd7 46
93f0822d
DB
47
48static inline int32_t mul_fp(int32_t x, int32_t y)
49{
50 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
51}
52
53static inline int32_t div_fp(int32_t x, int32_t y)
54{
55 return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
56}
57
58struct sample {
d253d2a5 59 int32_t core_pct_busy;
93f0822d
DB
60 u64 aperf;
61 u64 mperf;
62 int freq;
c4ee841f 63 ktime_t time;
93f0822d
DB
64};
65
66struct pstate_data {
67 int current_pstate;
68 int min_pstate;
69 int max_pstate;
70 int turbo_pstate;
71};
72
007bea09 73struct vid_data {
21855ff5
DB
74 int min;
75 int max;
76 int turbo;
007bea09
DB
77 int32_t ratio;
78};
79
93f0822d
DB
80struct _pid {
81 int setpoint;
82 int32_t integral;
83 int32_t p_gain;
84 int32_t i_gain;
85 int32_t d_gain;
86 int deadband;
d253d2a5 87 int32_t last_err;
93f0822d
DB
88};
89
90struct cpudata {
91 int cpu;
92
93 char name[64];
94
95 struct timer_list timer;
96
93f0822d 97 struct pstate_data pstate;
007bea09 98 struct vid_data vid;
93f0822d 99 struct _pid pid;
93f0822d 100
c4ee841f 101 ktime_t last_sample_time;
93f0822d
DB
102 u64 prev_aperf;
103 u64 prev_mperf;
d37e2b76 104 struct sample sample;
93f0822d
DB
105};
106
107static struct cpudata **all_cpu_data;
108struct pstate_adjust_policy {
109 int sample_rate_ms;
110 int deadband;
111 int setpoint;
112 int p_gain_pct;
113 int d_gain_pct;
114 int i_gain_pct;
115};
116
016c8150
DB
117struct pstate_funcs {
118 int (*get_max)(void);
119 int (*get_min)(void);
120 int (*get_turbo)(void);
007bea09
DB
121 void (*set)(struct cpudata*, int pstate);
122 void (*get_vid)(struct cpudata *);
93f0822d
DB
123};
124
016c8150
DB
125struct cpu_defaults {
126 struct pstate_adjust_policy pid_policy;
127 struct pstate_funcs funcs;
93f0822d
DB
128};
129
016c8150
DB
130static struct pstate_adjust_policy pid_params;
131static struct pstate_funcs pstate_funcs;
132
93f0822d
DB
133struct perf_limits {
134 int no_turbo;
135 int max_perf_pct;
136 int min_perf_pct;
137 int32_t max_perf;
138 int32_t min_perf;
d8f469e9
DB
139 int max_policy_pct;
140 int max_sysfs_pct;
93f0822d
DB
141};
142
143static struct perf_limits limits = {
144 .no_turbo = 0,
145 .max_perf_pct = 100,
146 .max_perf = int_tofp(1),
147 .min_perf_pct = 0,
148 .min_perf = 0,
d8f469e9
DB
149 .max_policy_pct = 100,
150 .max_sysfs_pct = 100,
93f0822d
DB
151};
152
153static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
154 int deadband, int integral) {
155 pid->setpoint = setpoint;
156 pid->deadband = deadband;
157 pid->integral = int_tofp(integral);
d98d099b 158 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
93f0822d
DB
159}
160
161static inline void pid_p_gain_set(struct _pid *pid, int percent)
162{
163 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
164}
165
166static inline void pid_i_gain_set(struct _pid *pid, int percent)
167{
168 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
169}
170
171static inline void pid_d_gain_set(struct _pid *pid, int percent)
172{
173
174 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
175}
176
d253d2a5 177static signed int pid_calc(struct _pid *pid, int32_t busy)
93f0822d 178{
d253d2a5 179 signed int result;
93f0822d
DB
180 int32_t pterm, dterm, fp_error;
181 int32_t integral_limit;
182
d253d2a5 183 fp_error = int_tofp(pid->setpoint) - busy;
93f0822d 184
d253d2a5 185 if (abs(fp_error) <= int_tofp(pid->deadband))
93f0822d
DB
186 return 0;
187
188 pterm = mul_fp(pid->p_gain, fp_error);
189
190 pid->integral += fp_error;
191
192 /* limit the integral term */
193 integral_limit = int_tofp(30);
194 if (pid->integral > integral_limit)
195 pid->integral = integral_limit;
196 if (pid->integral < -integral_limit)
197 pid->integral = -integral_limit;
198
d253d2a5
BS
199 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
200 pid->last_err = fp_error;
93f0822d
DB
201
202 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
f0fe3cd7
DB
203 if (result >= 0)
204 result = result + (1 << (FRAC_BITS-1));
205 else
206 result = result - (1 << (FRAC_BITS-1));
93f0822d
DB
207 return (signed int)fp_toint(result);
208}
209
210static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
211{
016c8150
DB
212 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
213 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
214 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
93f0822d
DB
215
216 pid_reset(&cpu->pid,
016c8150 217 pid_params.setpoint,
93f0822d 218 100,
016c8150 219 pid_params.deadband,
93f0822d
DB
220 0);
221}
222
93f0822d
DB
223static inline void intel_pstate_reset_all_pid(void)
224{
225 unsigned int cpu;
226 for_each_online_cpu(cpu) {
227 if (all_cpu_data[cpu])
228 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
229 }
230}
231
232/************************** debugfs begin ************************/
233static int pid_param_set(void *data, u64 val)
234{
235 *(u32 *)data = val;
236 intel_pstate_reset_all_pid();
237 return 0;
238}
239static int pid_param_get(void *data, u64 *val)
240{
241 *val = *(u32 *)data;
242 return 0;
243}
244DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
245 pid_param_set, "%llu\n");
246
247struct pid_param {
248 char *name;
249 void *value;
250};
251
252static struct pid_param pid_files[] = {
016c8150
DB
253 {"sample_rate_ms", &pid_params.sample_rate_ms},
254 {"d_gain_pct", &pid_params.d_gain_pct},
255 {"i_gain_pct", &pid_params.i_gain_pct},
256 {"deadband", &pid_params.deadband},
257 {"setpoint", &pid_params.setpoint},
258 {"p_gain_pct", &pid_params.p_gain_pct},
93f0822d
DB
259 {NULL, NULL}
260};
261
262static struct dentry *debugfs_parent;
263static void intel_pstate_debug_expose_params(void)
264{
265 int i = 0;
266
267 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
268 if (IS_ERR_OR_NULL(debugfs_parent))
269 return;
270 while (pid_files[i].name) {
271 debugfs_create_file(pid_files[i].name, 0660,
272 debugfs_parent, pid_files[i].value,
273 &fops_pid_param);
274 i++;
275 }
276}
277
278/************************** debugfs end ************************/
279
280/************************** sysfs begin ************************/
281#define show_one(file_name, object) \
282 static ssize_t show_##file_name \
283 (struct kobject *kobj, struct attribute *attr, char *buf) \
284 { \
285 return sprintf(buf, "%u\n", limits.object); \
286 }
287
288static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
289 const char *buf, size_t count)
290{
291 unsigned int input;
292 int ret;
293 ret = sscanf(buf, "%u", &input);
294 if (ret != 1)
295 return -EINVAL;
296 limits.no_turbo = clamp_t(int, input, 0 , 1);
297
298 return count;
299}
300
301static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
302 const char *buf, size_t count)
303{
304 unsigned int input;
305 int ret;
306 ret = sscanf(buf, "%u", &input);
307 if (ret != 1)
308 return -EINVAL;
309
d8f469e9
DB
310 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
311 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
93f0822d
DB
312 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
313 return count;
314}
315
316static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
317 const char *buf, size_t count)
318{
319 unsigned int input;
320 int ret;
321 ret = sscanf(buf, "%u", &input);
322 if (ret != 1)
323 return -EINVAL;
324 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
325 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
326
327 return count;
328}
329
330show_one(no_turbo, no_turbo);
331show_one(max_perf_pct, max_perf_pct);
332show_one(min_perf_pct, min_perf_pct);
333
334define_one_global_rw(no_turbo);
335define_one_global_rw(max_perf_pct);
336define_one_global_rw(min_perf_pct);
337
338static struct attribute *intel_pstate_attributes[] = {
339 &no_turbo.attr,
340 &max_perf_pct.attr,
341 &min_perf_pct.attr,
342 NULL
343};
344
345static struct attribute_group intel_pstate_attr_group = {
346 .attrs = intel_pstate_attributes,
347};
348static struct kobject *intel_pstate_kobject;
349
350static void intel_pstate_sysfs_expose_params(void)
351{
352 int rc;
353
354 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
355 &cpu_subsys.dev_root->kobj);
356 BUG_ON(!intel_pstate_kobject);
357 rc = sysfs_create_group(intel_pstate_kobject,
358 &intel_pstate_attr_group);
359 BUG_ON(rc);
360}
361
362/************************** sysfs end ************************/
19e77c28
DB
363static int byt_get_min_pstate(void)
364{
365 u64 value;
366 rdmsrl(BYT_RATIOS, value);
21855ff5 367 return (value >> 8) & 0x3F;
19e77c28
DB
368}
369
370static int byt_get_max_pstate(void)
371{
372 u64 value;
373 rdmsrl(BYT_RATIOS, value);
21855ff5 374 return (value >> 16) & 0x3F;
19e77c28 375}
93f0822d 376
61d8d2ab
DB
377static int byt_get_turbo_pstate(void)
378{
379 u64 value;
380 rdmsrl(BYT_TURBO_RATIOS, value);
381 return value & 0x3F;
382}
383
007bea09
DB
384static void byt_set_pstate(struct cpudata *cpudata, int pstate)
385{
386 u64 val;
387 int32_t vid_fp;
388 u32 vid;
389
390 val = pstate << 8;
391 if (limits.no_turbo)
392 val |= (u64)1 << 32;
393
394 vid_fp = cpudata->vid.min + mul_fp(
395 int_tofp(pstate - cpudata->pstate.min_pstate),
396 cpudata->vid.ratio);
397
398 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
399 vid = fp_toint(vid_fp);
400
21855ff5
DB
401 if (pstate > cpudata->pstate.max_pstate)
402 vid = cpudata->vid.turbo;
403
007bea09
DB
404 val |= vid;
405
406 wrmsrl(MSR_IA32_PERF_CTL, val);
407}
408
409static void byt_get_vid(struct cpudata *cpudata)
410{
411 u64 value;
412
21855ff5 413
007bea09 414 rdmsrl(BYT_VIDS, value);
21855ff5
DB
415 cpudata->vid.min = int_tofp((value >> 8) & 0x3f);
416 cpudata->vid.max = int_tofp((value >> 16) & 0x3f);
007bea09
DB
417 cpudata->vid.ratio = div_fp(
418 cpudata->vid.max - cpudata->vid.min,
419 int_tofp(cpudata->pstate.max_pstate -
420 cpudata->pstate.min_pstate));
21855ff5
DB
421
422 rdmsrl(BYT_TURBO_VIDS, value);
423 cpudata->vid.turbo = value & 0x7f;
007bea09
DB
424}
425
426
016c8150 427static int core_get_min_pstate(void)
93f0822d
DB
428{
429 u64 value;
05e99c8c 430 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
431 return (value >> 40) & 0xFF;
432}
433
016c8150 434static int core_get_max_pstate(void)
93f0822d
DB
435{
436 u64 value;
05e99c8c 437 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
438 return (value >> 8) & 0xFF;
439}
440
016c8150 441static int core_get_turbo_pstate(void)
93f0822d
DB
442{
443 u64 value;
444 int nont, ret;
05e99c8c 445 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
016c8150 446 nont = core_get_max_pstate();
93f0822d
DB
447 ret = ((value) & 255);
448 if (ret <= nont)
449 ret = nont;
450 return ret;
451}
452
007bea09 453static void core_set_pstate(struct cpudata *cpudata, int pstate)
016c8150
DB
454{
455 u64 val;
456
457 val = pstate << 8;
458 if (limits.no_turbo)
459 val |= (u64)1 << 32;
460
bb18008f 461 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
016c8150
DB
462}
463
464static struct cpu_defaults core_params = {
465 .pid_policy = {
466 .sample_rate_ms = 10,
467 .deadband = 0,
468 .setpoint = 97,
469 .p_gain_pct = 20,
470 .d_gain_pct = 0,
471 .i_gain_pct = 0,
472 },
473 .funcs = {
474 .get_max = core_get_max_pstate,
475 .get_min = core_get_min_pstate,
476 .get_turbo = core_get_turbo_pstate,
477 .set = core_set_pstate,
478 },
479};
480
19e77c28
DB
481static struct cpu_defaults byt_params = {
482 .pid_policy = {
483 .sample_rate_ms = 10,
484 .deadband = 0,
485 .setpoint = 97,
486 .p_gain_pct = 14,
487 .d_gain_pct = 0,
488 .i_gain_pct = 4,
489 },
490 .funcs = {
491 .get_max = byt_get_max_pstate,
492 .get_min = byt_get_min_pstate,
61d8d2ab 493 .get_turbo = byt_get_turbo_pstate,
007bea09
DB
494 .set = byt_set_pstate,
495 .get_vid = byt_get_vid,
19e77c28
DB
496 },
497};
498
499
93f0822d
DB
500static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
501{
502 int max_perf = cpu->pstate.turbo_pstate;
7244cb62 503 int max_perf_adj;
93f0822d
DB
504 int min_perf;
505 if (limits.no_turbo)
506 max_perf = cpu->pstate.max_pstate;
507
7244cb62
DB
508 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
509 *max = clamp_t(int, max_perf_adj,
93f0822d
DB
510 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
511
512 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
513 *min = clamp_t(int, min_perf,
514 cpu->pstate.min_pstate, max_perf);
515}
516
517static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
518{
519 int max_perf, min_perf;
520
521 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
522
523 pstate = clamp_t(int, pstate, min_perf, max_perf);
524
525 if (pstate == cpu->pstate.current_pstate)
526 return;
527
93f0822d 528 trace_cpu_frequency(pstate * 100000, cpu->cpu);
35363e94 529
93f0822d 530 cpu->pstate.current_pstate = pstate;
93f0822d 531
007bea09 532 pstate_funcs.set(cpu, pstate);
93f0822d
DB
533}
534
535static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
536{
537 int target;
538 target = cpu->pstate.current_pstate + steps;
539
540 intel_pstate_set_pstate(cpu, target);
541}
542
543static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
544{
545 int target;
546 target = cpu->pstate.current_pstate - steps;
547 intel_pstate_set_pstate(cpu, target);
548}
549
550static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
551{
552 sprintf(cpu->name, "Intel 2nd generation core");
553
016c8150
DB
554 cpu->pstate.min_pstate = pstate_funcs.get_min();
555 cpu->pstate.max_pstate = pstate_funcs.get_max();
556 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
93f0822d 557
007bea09
DB
558 if (pstate_funcs.get_vid)
559 pstate_funcs.get_vid(cpu);
d40a63c4 560 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
93f0822d
DB
561}
562
563static inline void intel_pstate_calc_busy(struct cpudata *cpu,
564 struct sample *sample)
565{
e66c1768 566 int32_t core_pct;
93f0822d 567
e66c1768
DB
568 core_pct = div_fp(int_tofp((sample->aperf)),
569 int_tofp((sample->mperf)));
570 core_pct = mul_fp(core_pct, int_tofp(100));
e66c1768 571
fcb6a15c 572 sample->freq = fp_toint(
e66c1768 573 mul_fp(int_tofp(cpu->pstate.max_pstate * 1000), core_pct));
fcb6a15c 574
adacdf3f 575 sample->core_pct_busy = core_pct;
93f0822d
DB
576}
577
578static inline void intel_pstate_sample(struct cpudata *cpu)
579{
93f0822d
DB
580 u64 aperf, mperf;
581
93f0822d
DB
582 rdmsrl(MSR_IA32_APERF, aperf);
583 rdmsrl(MSR_IA32_MPERF, mperf);
b69880f9 584
e66c1768
DB
585 aperf = aperf >> FRAC_BITS;
586 mperf = mperf >> FRAC_BITS;
e66c1768 587
c4ee841f
DB
588 cpu->last_sample_time = cpu->sample.time;
589 cpu->sample.time = ktime_get();
d37e2b76
DB
590 cpu->sample.aperf = aperf;
591 cpu->sample.mperf = mperf;
d37e2b76
DB
592 cpu->sample.aperf -= cpu->prev_aperf;
593 cpu->sample.mperf -= cpu->prev_mperf;
1abc4b20 594
d37e2b76 595 intel_pstate_calc_busy(cpu, &cpu->sample);
93f0822d 596
93f0822d
DB
597 cpu->prev_aperf = aperf;
598 cpu->prev_mperf = mperf;
599}
600
601static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
602{
603 int sample_time, delay;
604
016c8150 605 sample_time = pid_params.sample_rate_ms;
93f0822d 606 delay = msecs_to_jiffies(sample_time);
93f0822d
DB
607 mod_timer_pinned(&cpu->timer, jiffies + delay);
608}
609
d253d2a5 610static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
93f0822d 611{
c4ee841f
DB
612 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
613 u32 duration_us;
614 u32 sample_time;
93f0822d 615
d37e2b76 616 core_busy = cpu->sample.core_pct_busy;
2134ed4d 617 max_pstate = int_tofp(cpu->pstate.max_pstate);
93f0822d 618 current_pstate = int_tofp(cpu->pstate.current_pstate);
e66c1768 619 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
c4ee841f
DB
620
621 sample_time = (pid_params.sample_rate_ms * USEC_PER_MSEC);
622 duration_us = (u32) ktime_us_delta(cpu->sample.time,
623 cpu->last_sample_time);
624 if (duration_us > sample_time * 3) {
625 sample_ratio = div_fp(int_tofp(sample_time),
626 int_tofp(duration_us));
627 core_busy = mul_fp(core_busy, sample_ratio);
628 }
629
f0fe3cd7 630 return core_busy;
93f0822d
DB
631}
632
633static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
634{
d253d2a5 635 int32_t busy_scaled;
93f0822d
DB
636 struct _pid *pid;
637 signed int ctl = 0;
638 int steps;
639
640 pid = &cpu->pid;
641 busy_scaled = intel_pstate_get_scaled_busy(cpu);
642
643 ctl = pid_calc(pid, busy_scaled);
644
645 steps = abs(ctl);
b69880f9 646
93f0822d
DB
647 if (ctl < 0)
648 intel_pstate_pstate_increase(cpu, steps);
649 else
650 intel_pstate_pstate_decrease(cpu, steps);
651}
652
93f0822d
DB
653static void intel_pstate_timer_func(unsigned long __data)
654{
655 struct cpudata *cpu = (struct cpudata *) __data;
b69880f9 656 struct sample *sample;
93f0822d
DB
657
658 intel_pstate_sample(cpu);
b69880f9 659
d37e2b76 660 sample = &cpu->sample;
b69880f9 661
ca182aee 662 intel_pstate_adjust_busy_pstate(cpu);
b69880f9
DB
663
664 trace_pstate_sample(fp_toint(sample->core_pct_busy),
665 fp_toint(intel_pstate_get_scaled_busy(cpu)),
666 cpu->pstate.current_pstate,
667 sample->mperf,
668 sample->aperf,
b69880f9
DB
669 sample->freq);
670
93f0822d
DB
671 intel_pstate_set_sample_time(cpu);
672}
673
674#define ICPU(model, policy) \
6cbd7ee1
DB
675 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
676 (unsigned long)&policy }
93f0822d
DB
677
678static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
016c8150
DB
679 ICPU(0x2a, core_params),
680 ICPU(0x2d, core_params),
19e77c28 681 ICPU(0x37, byt_params),
016c8150
DB
682 ICPU(0x3a, core_params),
683 ICPU(0x3c, core_params),
684 ICPU(0x3e, core_params),
685 ICPU(0x3f, core_params),
686 ICPU(0x45, core_params),
687 ICPU(0x46, core_params),
93f0822d
DB
688 {}
689};
690MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
691
692static int intel_pstate_init_cpu(unsigned int cpunum)
693{
694
695 const struct x86_cpu_id *id;
696 struct cpudata *cpu;
697
698 id = x86_match_cpu(intel_pstate_cpu_ids);
699 if (!id)
700 return -ENODEV;
701
702 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
703 if (!all_cpu_data[cpunum])
704 return -ENOMEM;
705
706 cpu = all_cpu_data[cpunum];
707
708 intel_pstate_get_cpu_pstates(cpu);
709
710 cpu->cpu = cpunum;
016c8150 711
93f0822d
DB
712 init_timer_deferrable(&cpu->timer);
713 cpu->timer.function = intel_pstate_timer_func;
714 cpu->timer.data =
715 (unsigned long)cpu;
716 cpu->timer.expires = jiffies + HZ/100;
717 intel_pstate_busy_pid_reset(cpu);
93f0822d 718 intel_pstate_sample(cpu);
93f0822d
DB
719
720 add_timer_on(&cpu->timer, cpunum);
721
722 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
723
724 return 0;
725}
726
727static unsigned int intel_pstate_get(unsigned int cpu_num)
728{
729 struct sample *sample;
730 struct cpudata *cpu;
731
732 cpu = all_cpu_data[cpu_num];
733 if (!cpu)
734 return 0;
d37e2b76 735 sample = &cpu->sample;
93f0822d
DB
736 return sample->freq;
737}
738
739static int intel_pstate_set_policy(struct cpufreq_policy *policy)
740{
741 struct cpudata *cpu;
93f0822d
DB
742
743 cpu = all_cpu_data[policy->cpu];
744
d3929b83
DB
745 if (!policy->cpuinfo.max_freq)
746 return -ENODEV;
747
93f0822d
DB
748 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
749 limits.min_perf_pct = 100;
750 limits.min_perf = int_tofp(1);
751 limits.max_perf_pct = 100;
752 limits.max_perf = int_tofp(1);
753 limits.no_turbo = 0;
d1b68485 754 return 0;
93f0822d 755 }
d1b68485
SP
756 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
757 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
758 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
759
d8f469e9
DB
760 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
761 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
762 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
d1b68485 763 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
93f0822d
DB
764
765 return 0;
766}
767
768static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
769{
be49e346 770 cpufreq_verify_within_cpu_limits(policy);
93f0822d
DB
771
772 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
773 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
774 return -EINVAL;
775
776 return 0;
777}
778
bb18008f 779static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
93f0822d 780{
bb18008f
DB
781 int cpu_num = policy->cpu;
782 struct cpudata *cpu = all_cpu_data[cpu_num];
93f0822d 783
bb18008f
DB
784 pr_info("intel_pstate CPU %d exiting\n", cpu_num);
785
c2294a2f 786 del_timer_sync(&all_cpu_data[cpu_num]->timer);
bb18008f
DB
787 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
788 kfree(all_cpu_data[cpu_num]);
789 all_cpu_data[cpu_num] = NULL;
93f0822d
DB
790}
791
2760984f 792static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
93f0822d 793{
93f0822d 794 struct cpudata *cpu;
52e0a509 795 int rc;
93f0822d
DB
796
797 rc = intel_pstate_init_cpu(policy->cpu);
798 if (rc)
799 return rc;
800
801 cpu = all_cpu_data[policy->cpu];
802
803 if (!limits.no_turbo &&
804 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
805 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
806 else
807 policy->policy = CPUFREQ_POLICY_POWERSAVE;
808
52e0a509
DB
809 policy->min = cpu->pstate.min_pstate * 100000;
810 policy->max = cpu->pstate.turbo_pstate * 100000;
93f0822d
DB
811
812 /* cpuinfo and default policy values */
813 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
814 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
815 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
816 cpumask_set_cpu(policy->cpu, policy->cpus);
817
818 return 0;
819}
820
821static struct cpufreq_driver intel_pstate_driver = {
822 .flags = CPUFREQ_CONST_LOOPS,
823 .verify = intel_pstate_verify_policy,
824 .setpolicy = intel_pstate_set_policy,
825 .get = intel_pstate_get,
826 .init = intel_pstate_cpu_init,
bb18008f 827 .stop_cpu = intel_pstate_stop_cpu,
93f0822d 828 .name = "intel_pstate",
93f0822d
DB
829};
830
6be26498
DB
831static int __initdata no_load;
832
b563b4e3
DB
833static int intel_pstate_msrs_not_valid(void)
834{
835 /* Check that all the msr's we are using are valid. */
836 u64 aperf, mperf, tmp;
837
838 rdmsrl(MSR_IA32_APERF, aperf);
839 rdmsrl(MSR_IA32_MPERF, mperf);
840
016c8150
DB
841 if (!pstate_funcs.get_max() ||
842 !pstate_funcs.get_min() ||
843 !pstate_funcs.get_turbo())
b563b4e3
DB
844 return -ENODEV;
845
846 rdmsrl(MSR_IA32_APERF, tmp);
847 if (!(tmp - aperf))
848 return -ENODEV;
849
850 rdmsrl(MSR_IA32_MPERF, tmp);
851 if (!(tmp - mperf))
852 return -ENODEV;
853
854 return 0;
855}
016c8150 856
e0a261a2 857static void copy_pid_params(struct pstate_adjust_policy *policy)
016c8150
DB
858{
859 pid_params.sample_rate_ms = policy->sample_rate_ms;
860 pid_params.p_gain_pct = policy->p_gain_pct;
861 pid_params.i_gain_pct = policy->i_gain_pct;
862 pid_params.d_gain_pct = policy->d_gain_pct;
863 pid_params.deadband = policy->deadband;
864 pid_params.setpoint = policy->setpoint;
865}
866
e0a261a2 867static void copy_cpu_funcs(struct pstate_funcs *funcs)
016c8150
DB
868{
869 pstate_funcs.get_max = funcs->get_max;
870 pstate_funcs.get_min = funcs->get_min;
871 pstate_funcs.get_turbo = funcs->get_turbo;
872 pstate_funcs.set = funcs->set;
007bea09 873 pstate_funcs.get_vid = funcs->get_vid;
016c8150
DB
874}
875
fbbcdc07
AH
876#if IS_ENABLED(CONFIG_ACPI)
877#include <acpi/processor.h>
878
879static bool intel_pstate_no_acpi_pss(void)
880{
881 int i;
882
883 for_each_possible_cpu(i) {
884 acpi_status status;
885 union acpi_object *pss;
886 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
887 struct acpi_processor *pr = per_cpu(processors, i);
888
889 if (!pr)
890 continue;
891
892 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
893 if (ACPI_FAILURE(status))
894 continue;
895
896 pss = buffer.pointer;
897 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
898 kfree(pss);
899 return false;
900 }
901
902 kfree(pss);
903 }
904
905 return true;
906}
907
908struct hw_vendor_info {
909 u16 valid;
910 char oem_id[ACPI_OEM_ID_SIZE];
911 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
912};
913
914/* Hardware vendor-specific info that has its own power management modes */
915static struct hw_vendor_info vendor_info[] = {
916 {1, "HP ", "ProLiant"},
917 {0, "", ""},
918};
919
920static bool intel_pstate_platform_pwr_mgmt_exists(void)
921{
922 struct acpi_table_header hdr;
923 struct hw_vendor_info *v_info;
924
925 if (acpi_disabled
926 || ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
927 return false;
928
929 for (v_info = vendor_info; v_info->valid; v_info++) {
930 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE)
931 && !strncmp(hdr.oem_table_id, v_info->oem_table_id, ACPI_OEM_TABLE_ID_SIZE)
932 && intel_pstate_no_acpi_pss())
933 return true;
934 }
935
936 return false;
937}
938#else /* CONFIG_ACPI not enabled */
939static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
940#endif /* CONFIG_ACPI */
941
93f0822d
DB
942static int __init intel_pstate_init(void)
943{
907cc908 944 int cpu, rc = 0;
93f0822d 945 const struct x86_cpu_id *id;
016c8150 946 struct cpu_defaults *cpu_info;
93f0822d 947
6be26498
DB
948 if (no_load)
949 return -ENODEV;
950
93f0822d
DB
951 id = x86_match_cpu(intel_pstate_cpu_ids);
952 if (!id)
953 return -ENODEV;
954
fbbcdc07
AH
955 /*
956 * The Intel pstate driver will be ignored if the platform
957 * firmware has its own power management modes.
958 */
959 if (intel_pstate_platform_pwr_mgmt_exists())
960 return -ENODEV;
961
016c8150
DB
962 cpu_info = (struct cpu_defaults *)id->driver_data;
963
964 copy_pid_params(&cpu_info->pid_policy);
965 copy_cpu_funcs(&cpu_info->funcs);
966
b563b4e3
DB
967 if (intel_pstate_msrs_not_valid())
968 return -ENODEV;
969
93f0822d
DB
970 pr_info("Intel P-state driver initializing.\n");
971
b57ffac5 972 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
93f0822d
DB
973 if (!all_cpu_data)
974 return -ENOMEM;
93f0822d
DB
975
976 rc = cpufreq_register_driver(&intel_pstate_driver);
977 if (rc)
978 goto out;
979
980 intel_pstate_debug_expose_params();
981 intel_pstate_sysfs_expose_params();
b69880f9 982
93f0822d
DB
983 return rc;
984out:
907cc908
DB
985 get_online_cpus();
986 for_each_online_cpu(cpu) {
987 if (all_cpu_data[cpu]) {
988 del_timer_sync(&all_cpu_data[cpu]->timer);
989 kfree(all_cpu_data[cpu]);
990 }
991 }
992
993 put_online_cpus();
994 vfree(all_cpu_data);
93f0822d
DB
995 return -ENODEV;
996}
997device_initcall(intel_pstate_init);
998
6be26498
DB
999static int __init intel_pstate_setup(char *str)
1000{
1001 if (!str)
1002 return -EINVAL;
1003
1004 if (!strcmp(str, "disable"))
1005 no_load = 1;
1006 return 0;
1007}
1008early_param("intel_pstate", intel_pstate_setup);
1009
93f0822d
DB
1010MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1011MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1012MODULE_LICENSE("GPL");
This page took 0.131002 seconds and 5 git commands to generate.