2 * intel_pstate.c: Native P state management for Intel processors
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
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
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>
27 #include <linux/debugfs.h>
28 #include <linux/acpi.h>
29 #include <trace/events/power.h>
31 #include <asm/div64.h>
33 #include <asm/cpu_device_id.h>
34 #include <asm/cpufeature.h>
36 #define BYT_RATIOS 0x66a
37 #define BYT_VIDS 0x66b
38 #define BYT_TURBO_RATIOS 0x66c
39 #define BYT_TURBO_VIDS 0x66d
42 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
43 #define fp_toint(X) ((X) >> FRAC_BITS)
46 static inline int32_t mul_fp(int32_t x, int32_t y)
48 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
51 static inline int32_t div_fp(int32_t x, int32_t y)
53 return div_s64((int64_t)x << FRAC_BITS, y);
56 static inline int ceiling_fp(int32_t x)
61 mask = (1 << FRAC_BITS) - 1;
68 int32_t core_pct_busy;
104 struct timer_list timer;
106 struct pstate_data pstate;
110 ktime_t last_sample_time;
114 struct sample sample;
117 static struct cpudata **all_cpu_data;
118 struct pstate_adjust_policy {
127 struct pstate_funcs {
128 int (*get_max)(void);
129 int (*get_min)(void);
130 int (*get_turbo)(void);
131 int (*get_scaling)(void);
132 void (*set)(struct cpudata*, int pstate);
133 void (*get_vid)(struct cpudata *);
136 struct cpu_defaults {
137 struct pstate_adjust_policy pid_policy;
138 struct pstate_funcs funcs;
141 static struct pstate_adjust_policy pid_params;
142 static struct pstate_funcs pstate_funcs;
143 static int hwp_active;
158 static struct perf_limits limits = {
162 .max_perf = int_tofp(1),
165 .max_policy_pct = 100,
166 .max_sysfs_pct = 100,
171 static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
172 int deadband, int integral) {
173 pid->setpoint = setpoint;
174 pid->deadband = deadband;
175 pid->integral = int_tofp(integral);
176 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
179 static inline void pid_p_gain_set(struct _pid *pid, int percent)
181 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
184 static inline void pid_i_gain_set(struct _pid *pid, int percent)
186 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
189 static inline void pid_d_gain_set(struct _pid *pid, int percent)
191 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
194 static signed int pid_calc(struct _pid *pid, int32_t busy)
197 int32_t pterm, dterm, fp_error;
198 int32_t integral_limit;
200 fp_error = int_tofp(pid->setpoint) - busy;
202 if (abs(fp_error) <= int_tofp(pid->deadband))
205 pterm = mul_fp(pid->p_gain, fp_error);
207 pid->integral += fp_error;
210 * We limit the integral here so that it will never
211 * get higher than 30. This prevents it from becoming
212 * too large an input over long periods of time and allows
213 * it to get factored out sooner.
215 * The value of 30 was chosen through experimentation.
217 integral_limit = int_tofp(30);
218 if (pid->integral > integral_limit)
219 pid->integral = integral_limit;
220 if (pid->integral < -integral_limit)
221 pid->integral = -integral_limit;
223 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
224 pid->last_err = fp_error;
226 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
227 result = result + (1 << (FRAC_BITS-1));
228 return (signed int)fp_toint(result);
231 static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
233 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
234 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
235 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
237 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
240 static inline void intel_pstate_reset_all_pid(void)
244 for_each_online_cpu(cpu) {
245 if (all_cpu_data[cpu])
246 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
250 static inline void update_turbo_state(void)
255 cpu = all_cpu_data[0];
256 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
257 limits.turbo_disabled =
258 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
259 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
262 #define PCT_TO_HWP(x) (x * 255 / 100)
263 static void intel_pstate_hwp_set(void)
270 for_each_online_cpu(cpu) {
271 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
272 min = PCT_TO_HWP(limits.min_perf_pct);
273 value &= ~HWP_MIN_PERF(~0L);
274 value |= HWP_MIN_PERF(min);
276 max = PCT_TO_HWP(limits.max_perf_pct);
277 if (limits.no_turbo) {
278 rdmsrl( MSR_HWP_CAPABILITIES, freq);
279 max = HWP_GUARANTEED_PERF(freq);
282 value &= ~HWP_MAX_PERF(~0L);
283 value |= HWP_MAX_PERF(max);
284 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
290 /************************** debugfs begin ************************/
291 static int pid_param_set(void *data, u64 val)
294 intel_pstate_reset_all_pid();
298 static int pid_param_get(void *data, u64 *val)
303 DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
310 static struct pid_param pid_files[] = {
311 {"sample_rate_ms", &pid_params.sample_rate_ms},
312 {"d_gain_pct", &pid_params.d_gain_pct},
313 {"i_gain_pct", &pid_params.i_gain_pct},
314 {"deadband", &pid_params.deadband},
315 {"setpoint", &pid_params.setpoint},
316 {"p_gain_pct", &pid_params.p_gain_pct},
320 static void __init intel_pstate_debug_expose_params(void)
322 struct dentry *debugfs_parent;
327 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
328 if (IS_ERR_OR_NULL(debugfs_parent))
330 while (pid_files[i].name) {
331 debugfs_create_file(pid_files[i].name, 0660,
332 debugfs_parent, pid_files[i].value,
338 /************************** debugfs end ************************/
340 /************************** sysfs begin ************************/
341 #define show_one(file_name, object) \
342 static ssize_t show_##file_name \
343 (struct kobject *kobj, struct attribute *attr, char *buf) \
345 return sprintf(buf, "%u\n", limits.object); \
348 static ssize_t show_turbo_pct(struct kobject *kobj,
349 struct attribute *attr, char *buf)
352 int total, no_turbo, turbo_pct;
355 cpu = all_cpu_data[0];
357 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
358 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
359 turbo_fp = div_fp(int_tofp(no_turbo), int_tofp(total));
360 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
361 return sprintf(buf, "%u\n", turbo_pct);
364 static ssize_t show_num_pstates(struct kobject *kobj,
365 struct attribute *attr, char *buf)
370 cpu = all_cpu_data[0];
371 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
372 return sprintf(buf, "%u\n", total);
375 static ssize_t show_no_turbo(struct kobject *kobj,
376 struct attribute *attr, char *buf)
380 update_turbo_state();
381 if (limits.turbo_disabled)
382 ret = sprintf(buf, "%u\n", limits.turbo_disabled);
384 ret = sprintf(buf, "%u\n", limits.no_turbo);
389 static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
390 const char *buf, size_t count)
395 ret = sscanf(buf, "%u", &input);
399 update_turbo_state();
400 if (limits.turbo_disabled) {
401 pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n");
405 limits.no_turbo = clamp_t(int, input, 0, 1);
408 intel_pstate_hwp_set();
413 static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
414 const char *buf, size_t count)
419 ret = sscanf(buf, "%u", &input);
423 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
424 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
425 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
428 intel_pstate_hwp_set();
432 static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
433 const char *buf, size_t count)
438 ret = sscanf(buf, "%u", &input);
442 limits.min_sysfs_pct = clamp_t(int, input, 0 , 100);
443 limits.min_perf_pct = max(limits.min_policy_pct, limits.min_sysfs_pct);
444 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
447 intel_pstate_hwp_set();
451 show_one(max_perf_pct, max_perf_pct);
452 show_one(min_perf_pct, min_perf_pct);
454 define_one_global_rw(no_turbo);
455 define_one_global_rw(max_perf_pct);
456 define_one_global_rw(min_perf_pct);
457 define_one_global_ro(turbo_pct);
458 define_one_global_ro(num_pstates);
460 static struct attribute *intel_pstate_attributes[] = {
469 static struct attribute_group intel_pstate_attr_group = {
470 .attrs = intel_pstate_attributes,
473 static void __init intel_pstate_sysfs_expose_params(void)
475 struct kobject *intel_pstate_kobject;
478 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
479 &cpu_subsys.dev_root->kobj);
480 BUG_ON(!intel_pstate_kobject);
481 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
484 /************************** sysfs end ************************/
486 static void intel_pstate_hwp_enable(void)
489 pr_info("intel_pstate: HWP enabled\n");
491 wrmsrl( MSR_PM_ENABLE, 0x1);
494 static int byt_get_min_pstate(void)
498 rdmsrl(BYT_RATIOS, value);
499 return (value >> 8) & 0x7F;
502 static int byt_get_max_pstate(void)
506 rdmsrl(BYT_RATIOS, value);
507 return (value >> 16) & 0x7F;
510 static int byt_get_turbo_pstate(void)
514 rdmsrl(BYT_TURBO_RATIOS, value);
518 static void byt_set_pstate(struct cpudata *cpudata, int pstate)
525 if (limits.no_turbo && !limits.turbo_disabled)
528 vid_fp = cpudata->vid.min + mul_fp(
529 int_tofp(pstate - cpudata->pstate.min_pstate),
532 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
533 vid = ceiling_fp(vid_fp);
535 if (pstate > cpudata->pstate.max_pstate)
536 vid = cpudata->vid.turbo;
540 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
543 #define BYT_BCLK_FREQS 5
544 static int byt_freq_table[BYT_BCLK_FREQS] = { 833, 1000, 1333, 1167, 800};
546 static int byt_get_scaling(void)
551 rdmsrl(MSR_FSB_FREQ, value);
554 BUG_ON(i > BYT_BCLK_FREQS);
556 return byt_freq_table[i] * 100;
559 static void byt_get_vid(struct cpudata *cpudata)
563 rdmsrl(BYT_VIDS, value);
564 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
565 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
566 cpudata->vid.ratio = div_fp(
567 cpudata->vid.max - cpudata->vid.min,
568 int_tofp(cpudata->pstate.max_pstate -
569 cpudata->pstate.min_pstate));
571 rdmsrl(BYT_TURBO_VIDS, value);
572 cpudata->vid.turbo = value & 0x7f;
575 static int core_get_min_pstate(void)
579 rdmsrl(MSR_PLATFORM_INFO, value);
580 return (value >> 40) & 0xFF;
583 static int core_get_max_pstate(void)
587 rdmsrl(MSR_PLATFORM_INFO, value);
588 return (value >> 8) & 0xFF;
591 static int core_get_turbo_pstate(void)
596 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
597 nont = core_get_max_pstate();
604 static inline int core_get_scaling(void)
609 static void core_set_pstate(struct cpudata *cpudata, int pstate)
614 if (limits.no_turbo && !limits.turbo_disabled)
617 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
620 static int knl_get_turbo_pstate(void)
625 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
626 nont = core_get_max_pstate();
627 ret = (((value) >> 8) & 0xFF);
633 static struct cpu_defaults core_params = {
635 .sample_rate_ms = 10,
643 .get_max = core_get_max_pstate,
644 .get_min = core_get_min_pstate,
645 .get_turbo = core_get_turbo_pstate,
646 .get_scaling = core_get_scaling,
647 .set = core_set_pstate,
651 static struct cpu_defaults byt_params = {
653 .sample_rate_ms = 10,
661 .get_max = byt_get_max_pstate,
662 .get_min = byt_get_min_pstate,
663 .get_turbo = byt_get_turbo_pstate,
664 .set = byt_set_pstate,
665 .get_scaling = byt_get_scaling,
666 .get_vid = byt_get_vid,
670 static struct cpu_defaults knl_params = {
672 .sample_rate_ms = 10,
680 .get_max = core_get_max_pstate,
681 .get_min = core_get_min_pstate,
682 .get_turbo = knl_get_turbo_pstate,
683 .set = core_set_pstate,
687 static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
689 int max_perf = cpu->pstate.turbo_pstate;
693 if (limits.no_turbo || limits.turbo_disabled)
694 max_perf = cpu->pstate.max_pstate;
697 * performance can be limited by user through sysfs, by cpufreq
698 * policy, or by cpu specific default values determined through
701 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
702 *max = clamp_t(int, max_perf_adj,
703 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
705 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
706 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
709 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force)
711 int max_perf, min_perf;
714 update_turbo_state();
716 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
718 pstate = clamp_t(int, pstate, min_perf, max_perf);
720 if (pstate == cpu->pstate.current_pstate)
723 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
725 cpu->pstate.current_pstate = pstate;
727 pstate_funcs.set(cpu, pstate);
730 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
732 cpu->pstate.min_pstate = pstate_funcs.get_min();
733 cpu->pstate.max_pstate = pstate_funcs.get_max();
734 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
735 cpu->pstate.scaling = pstate_funcs.get_scaling();
737 if (pstate_funcs.get_vid)
738 pstate_funcs.get_vid(cpu);
739 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
742 static inline void intel_pstate_calc_busy(struct cpudata *cpu)
744 struct sample *sample = &cpu->sample;
747 core_pct = int_tofp(sample->aperf) * int_tofp(100);
748 core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
750 sample->freq = fp_toint(
752 cpu->pstate.max_pstate * cpu->pstate.scaling / 100),
755 sample->core_pct_busy = (int32_t)core_pct;
758 static inline void intel_pstate_sample(struct cpudata *cpu)
764 local_irq_save(flags);
765 rdmsrl(MSR_IA32_APERF, aperf);
766 rdmsrl(MSR_IA32_MPERF, mperf);
767 tsc = native_read_tsc();
768 local_irq_restore(flags);
770 cpu->last_sample_time = cpu->sample.time;
771 cpu->sample.time = ktime_get();
772 cpu->sample.aperf = aperf;
773 cpu->sample.mperf = mperf;
774 cpu->sample.tsc = tsc;
775 cpu->sample.aperf -= cpu->prev_aperf;
776 cpu->sample.mperf -= cpu->prev_mperf;
777 cpu->sample.tsc -= cpu->prev_tsc;
779 intel_pstate_calc_busy(cpu);
781 cpu->prev_aperf = aperf;
782 cpu->prev_mperf = mperf;
786 static inline void intel_hwp_set_sample_time(struct cpudata *cpu)
790 delay = msecs_to_jiffies(50);
791 mod_timer_pinned(&cpu->timer, jiffies + delay);
794 static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
798 delay = msecs_to_jiffies(pid_params.sample_rate_ms);
799 mod_timer_pinned(&cpu->timer, jiffies + delay);
802 static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
804 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
809 * core_busy is the ratio of actual performance to max
810 * max_pstate is the max non turbo pstate available
811 * current_pstate was the pstate that was requested during
812 * the last sample period.
814 * We normalize core_busy, which was our actual percent
815 * performance to what we requested during the last sample
816 * period. The result will be a percentage of busy at a
819 core_busy = cpu->sample.core_pct_busy;
820 max_pstate = int_tofp(cpu->pstate.max_pstate);
821 current_pstate = int_tofp(cpu->pstate.current_pstate);
822 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
825 * Since we have a deferred timer, it will not fire unless
826 * we are in C0. So, determine if the actual elapsed time
827 * is significantly greater (3x) than our sample interval. If it
828 * is, then we were idle for a long enough period of time
829 * to adjust our busyness.
831 sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC;
832 duration_us = (u32) ktime_us_delta(cpu->sample.time,
833 cpu->last_sample_time);
834 if (duration_us > sample_time * 3) {
835 sample_ratio = div_fp(int_tofp(sample_time),
836 int_tofp(duration_us));
837 core_busy = mul_fp(core_busy, sample_ratio);
843 static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
849 struct sample *sample;
851 from = cpu->pstate.current_pstate;
854 busy_scaled = intel_pstate_get_scaled_busy(cpu);
856 ctl = pid_calc(pid, busy_scaled);
858 /* Negative values of ctl increase the pstate and vice versa */
859 intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl, true);
861 sample = &cpu->sample;
862 trace_pstate_sample(fp_toint(sample->core_pct_busy),
863 fp_toint(busy_scaled),
865 cpu->pstate.current_pstate,
872 static void intel_hwp_timer_func(unsigned long __data)
874 struct cpudata *cpu = (struct cpudata *) __data;
876 intel_pstate_sample(cpu);
877 intel_hwp_set_sample_time(cpu);
880 static void intel_pstate_timer_func(unsigned long __data)
882 struct cpudata *cpu = (struct cpudata *) __data;
884 intel_pstate_sample(cpu);
886 intel_pstate_adjust_busy_pstate(cpu);
888 intel_pstate_set_sample_time(cpu);
891 #define ICPU(model, policy) \
892 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
893 (unsigned long)&policy }
895 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
896 ICPU(0x2a, core_params),
897 ICPU(0x2d, core_params),
898 ICPU(0x37, byt_params),
899 ICPU(0x3a, core_params),
900 ICPU(0x3c, core_params),
901 ICPU(0x3d, core_params),
902 ICPU(0x3e, core_params),
903 ICPU(0x3f, core_params),
904 ICPU(0x45, core_params),
905 ICPU(0x46, core_params),
906 ICPU(0x47, core_params),
907 ICPU(0x4c, byt_params),
908 ICPU(0x4e, core_params),
909 ICPU(0x4f, core_params),
910 ICPU(0x56, core_params),
911 ICPU(0x57, knl_params),
914 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
916 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
917 ICPU(0x56, core_params),
921 static int intel_pstate_init_cpu(unsigned int cpunum)
925 if (!all_cpu_data[cpunum])
926 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
928 if (!all_cpu_data[cpunum])
931 cpu = all_cpu_data[cpunum];
934 intel_pstate_get_cpu_pstates(cpu);
936 init_timer_deferrable(&cpu->timer);
937 cpu->timer.data = (unsigned long)cpu;
938 cpu->timer.expires = jiffies + HZ/100;
941 cpu->timer.function = intel_pstate_timer_func;
943 cpu->timer.function = intel_hwp_timer_func;
945 intel_pstate_busy_pid_reset(cpu);
946 intel_pstate_sample(cpu);
948 add_timer_on(&cpu->timer, cpunum);
950 pr_debug("intel_pstate: controlling: cpu %d\n", cpunum);
955 static unsigned int intel_pstate_get(unsigned int cpu_num)
957 struct sample *sample;
960 cpu = all_cpu_data[cpu_num];
963 sample = &cpu->sample;
967 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
969 if (!policy->cpuinfo.max_freq)
972 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE &&
973 policy->max >= policy->cpuinfo.max_freq) {
974 limits.min_policy_pct = 100;
975 limits.min_perf_pct = 100;
976 limits.min_perf = int_tofp(1);
977 limits.max_policy_pct = 100;
978 limits.max_perf_pct = 100;
979 limits.max_perf = int_tofp(1);
984 limits.min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
985 limits.min_policy_pct = clamp_t(int, limits.min_policy_pct, 0 , 100);
986 limits.min_perf_pct = max(limits.min_policy_pct, limits.min_sysfs_pct);
987 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
989 limits.max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
990 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
991 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
992 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
995 intel_pstate_hwp_set();
1000 static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1002 cpufreq_verify_within_cpu_limits(policy);
1004 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
1005 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
1011 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1013 int cpu_num = policy->cpu;
1014 struct cpudata *cpu = all_cpu_data[cpu_num];
1016 pr_debug("intel_pstate: CPU %d exiting\n", cpu_num);
1018 del_timer_sync(&all_cpu_data[cpu_num]->timer);
1022 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
1025 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
1027 struct cpudata *cpu;
1030 rc = intel_pstate_init_cpu(policy->cpu);
1034 cpu = all_cpu_data[policy->cpu];
1036 if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
1037 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1039 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1041 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1042 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1044 /* cpuinfo and default policy values */
1045 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
1046 policy->cpuinfo.max_freq =
1047 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1048 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1049 cpumask_set_cpu(policy->cpu, policy->cpus);
1054 static struct cpufreq_driver intel_pstate_driver = {
1055 .flags = CPUFREQ_CONST_LOOPS,
1056 .verify = intel_pstate_verify_policy,
1057 .setpolicy = intel_pstate_set_policy,
1058 .get = intel_pstate_get,
1059 .init = intel_pstate_cpu_init,
1060 .stop_cpu = intel_pstate_stop_cpu,
1061 .name = "intel_pstate",
1064 static int __initdata no_load;
1065 static int __initdata no_hwp;
1066 static int __initdata hwp_only;
1067 static unsigned int force_load;
1069 static int intel_pstate_msrs_not_valid(void)
1071 if (!pstate_funcs.get_max() ||
1072 !pstate_funcs.get_min() ||
1073 !pstate_funcs.get_turbo())
1079 static void copy_pid_params(struct pstate_adjust_policy *policy)
1081 pid_params.sample_rate_ms = policy->sample_rate_ms;
1082 pid_params.p_gain_pct = policy->p_gain_pct;
1083 pid_params.i_gain_pct = policy->i_gain_pct;
1084 pid_params.d_gain_pct = policy->d_gain_pct;
1085 pid_params.deadband = policy->deadband;
1086 pid_params.setpoint = policy->setpoint;
1089 static void copy_cpu_funcs(struct pstate_funcs *funcs)
1091 pstate_funcs.get_max = funcs->get_max;
1092 pstate_funcs.get_min = funcs->get_min;
1093 pstate_funcs.get_turbo = funcs->get_turbo;
1094 pstate_funcs.get_scaling = funcs->get_scaling;
1095 pstate_funcs.set = funcs->set;
1096 pstate_funcs.get_vid = funcs->get_vid;
1099 #if IS_ENABLED(CONFIG_ACPI)
1100 #include <acpi/processor.h>
1102 static bool intel_pstate_no_acpi_pss(void)
1106 for_each_possible_cpu(i) {
1108 union acpi_object *pss;
1109 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1110 struct acpi_processor *pr = per_cpu(processors, i);
1115 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
1116 if (ACPI_FAILURE(status))
1119 pss = buffer.pointer;
1120 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
1131 static bool intel_pstate_has_acpi_ppc(void)
1135 for_each_possible_cpu(i) {
1136 struct acpi_processor *pr = per_cpu(processors, i);
1140 if (acpi_has_method(pr->handle, "_PPC"))
1151 struct hw_vendor_info {
1153 char oem_id[ACPI_OEM_ID_SIZE];
1154 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
1158 /* Hardware vendor-specific info that has its own power management modes */
1159 static struct hw_vendor_info vendor_info[] = {
1160 {1, "HP ", "ProLiant", PSS},
1161 {1, "ORACLE", "X4-2 ", PPC},
1162 {1, "ORACLE", "X4-2L ", PPC},
1163 {1, "ORACLE", "X4-2B ", PPC},
1164 {1, "ORACLE", "X3-2 ", PPC},
1165 {1, "ORACLE", "X3-2L ", PPC},
1166 {1, "ORACLE", "X3-2B ", PPC},
1167 {1, "ORACLE", "X4470M2 ", PPC},
1168 {1, "ORACLE", "X4270M3 ", PPC},
1169 {1, "ORACLE", "X4270M2 ", PPC},
1170 {1, "ORACLE", "X4170M2 ", PPC},
1174 static bool intel_pstate_platform_pwr_mgmt_exists(void)
1176 struct acpi_table_header hdr;
1177 struct hw_vendor_info *v_info;
1178 const struct x86_cpu_id *id;
1181 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
1183 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
1184 if ( misc_pwr & (1 << 8))
1188 if (acpi_disabled ||
1189 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
1192 for (v_info = vendor_info; v_info->valid; v_info++) {
1193 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
1194 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
1195 ACPI_OEM_TABLE_ID_SIZE))
1196 switch (v_info->oem_pwr_table) {
1198 return intel_pstate_no_acpi_pss();
1200 return intel_pstate_has_acpi_ppc() &&
1207 #else /* CONFIG_ACPI not enabled */
1208 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
1209 static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
1210 #endif /* CONFIG_ACPI */
1212 static int __init intel_pstate_init(void)
1215 const struct x86_cpu_id *id;
1216 struct cpu_defaults *cpu_def;
1221 id = x86_match_cpu(intel_pstate_cpu_ids);
1226 * The Intel pstate driver will be ignored if the platform
1227 * firmware has its own power management modes.
1229 if (intel_pstate_platform_pwr_mgmt_exists())
1232 cpu_def = (struct cpu_defaults *)id->driver_data;
1234 copy_pid_params(&cpu_def->pid_policy);
1235 copy_cpu_funcs(&cpu_def->funcs);
1237 if (intel_pstate_msrs_not_valid())
1240 pr_info("Intel P-state driver initializing.\n");
1242 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
1246 if (static_cpu_has_safe(X86_FEATURE_HWP) && !no_hwp)
1247 intel_pstate_hwp_enable();
1249 if (!hwp_active && hwp_only)
1252 rc = cpufreq_register_driver(&intel_pstate_driver);
1256 intel_pstate_debug_expose_params();
1257 intel_pstate_sysfs_expose_params();
1262 for_each_online_cpu(cpu) {
1263 if (all_cpu_data[cpu]) {
1264 del_timer_sync(&all_cpu_data[cpu]->timer);
1265 kfree(all_cpu_data[cpu]);
1270 vfree(all_cpu_data);
1273 device_initcall(intel_pstate_init);
1275 static int __init intel_pstate_setup(char *str)
1280 if (!strcmp(str, "disable"))
1282 if (!strcmp(str, "no_hwp"))
1284 if (!strcmp(str, "force"))
1286 if (!strcmp(str, "hwp_only"))
1290 early_param("intel_pstate", intel_pstate_setup);
1292 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1293 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1294 MODULE_LICENSE("GPL");