Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1 | /* |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 2 | * intel_pstate.c: Native P state management for Intel processors |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 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 | |
| 40 | static inline int32_t mul_fp(int32_t x, int32_t y) |
| 41 | { |
| 42 | return ((int64_t)x * (int64_t)y) >> FRAC_BITS; |
| 43 | } |
| 44 | |
| 45 | static 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 | |
| 50 | struct sample { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 51 | int core_pct_busy; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 52 | u64 aperf; |
| 53 | u64 mperf; |
| 54 | int freq; |
| 55 | }; |
| 56 | |
| 57 | struct pstate_data { |
| 58 | int current_pstate; |
| 59 | int min_pstate; |
| 60 | int max_pstate; |
| 61 | int turbo_pstate; |
| 62 | }; |
| 63 | |
| 64 | struct _pid { |
| 65 | int setpoint; |
| 66 | int32_t integral; |
| 67 | int32_t p_gain; |
| 68 | int32_t i_gain; |
| 69 | int32_t d_gain; |
| 70 | int deadband; |
| 71 | int last_err; |
| 72 | }; |
| 73 | |
| 74 | struct cpudata { |
| 75 | int cpu; |
| 76 | |
| 77 | char name[64]; |
| 78 | |
| 79 | struct timer_list timer; |
| 80 | |
| 81 | struct pstate_adjust_policy *pstate_policy; |
| 82 | struct pstate_data pstate; |
| 83 | struct _pid pid; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 84 | |
| 85 | int min_pstate_count; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 86 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 87 | u64 prev_aperf; |
| 88 | u64 prev_mperf; |
| 89 | int sample_ptr; |
| 90 | struct sample samples[SAMPLE_COUNT]; |
| 91 | }; |
| 92 | |
| 93 | static struct cpudata **all_cpu_data; |
| 94 | struct pstate_adjust_policy { |
| 95 | int sample_rate_ms; |
| 96 | int deadband; |
| 97 | int setpoint; |
| 98 | int p_gain_pct; |
| 99 | int d_gain_pct; |
| 100 | int i_gain_pct; |
| 101 | }; |
| 102 | |
| 103 | static struct pstate_adjust_policy default_policy = { |
| 104 | .sample_rate_ms = 10, |
| 105 | .deadband = 0, |
Dirk Brandewie | 2134ed4 | 2013-07-18 08:48:42 -0700 | [diff] [blame] | 106 | .setpoint = 97, |
| 107 | .p_gain_pct = 20, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 108 | .d_gain_pct = 0, |
Dirk Brandewie | 2134ed4 | 2013-07-18 08:48:42 -0700 | [diff] [blame] | 109 | .i_gain_pct = 0, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct perf_limits { |
| 113 | int no_turbo; |
| 114 | int max_perf_pct; |
| 115 | int min_perf_pct; |
| 116 | int32_t max_perf; |
| 117 | int32_t min_perf; |
Dirk Brandewie | d8f469e | 2013-05-07 08:20:26 -0700 | [diff] [blame] | 118 | int max_policy_pct; |
| 119 | int max_sysfs_pct; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static struct perf_limits limits = { |
| 123 | .no_turbo = 0, |
| 124 | .max_perf_pct = 100, |
| 125 | .max_perf = int_tofp(1), |
| 126 | .min_perf_pct = 0, |
| 127 | .min_perf = 0, |
Dirk Brandewie | d8f469e | 2013-05-07 08:20:26 -0700 | [diff] [blame] | 128 | .max_policy_pct = 100, |
| 129 | .max_sysfs_pct = 100, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static inline void pid_reset(struct _pid *pid, int setpoint, int busy, |
| 133 | int deadband, int integral) { |
| 134 | pid->setpoint = setpoint; |
| 135 | pid->deadband = deadband; |
| 136 | pid->integral = int_tofp(integral); |
| 137 | pid->last_err = setpoint - busy; |
| 138 | } |
| 139 | |
| 140 | static inline void pid_p_gain_set(struct _pid *pid, int percent) |
| 141 | { |
| 142 | pid->p_gain = div_fp(int_tofp(percent), int_tofp(100)); |
| 143 | } |
| 144 | |
| 145 | static inline void pid_i_gain_set(struct _pid *pid, int percent) |
| 146 | { |
| 147 | pid->i_gain = div_fp(int_tofp(percent), int_tofp(100)); |
| 148 | } |
| 149 | |
| 150 | static inline void pid_d_gain_set(struct _pid *pid, int percent) |
| 151 | { |
| 152 | |
| 153 | pid->d_gain = div_fp(int_tofp(percent), int_tofp(100)); |
| 154 | } |
| 155 | |
| 156 | static signed int pid_calc(struct _pid *pid, int busy) |
| 157 | { |
| 158 | signed int err, result; |
| 159 | int32_t pterm, dterm, fp_error; |
| 160 | int32_t integral_limit; |
| 161 | |
| 162 | err = pid->setpoint - busy; |
| 163 | fp_error = int_tofp(err); |
| 164 | |
| 165 | if (abs(err) <= pid->deadband) |
| 166 | return 0; |
| 167 | |
| 168 | pterm = mul_fp(pid->p_gain, fp_error); |
| 169 | |
| 170 | pid->integral += fp_error; |
| 171 | |
| 172 | /* limit the integral term */ |
| 173 | integral_limit = int_tofp(30); |
| 174 | if (pid->integral > integral_limit) |
| 175 | pid->integral = integral_limit; |
| 176 | if (pid->integral < -integral_limit) |
| 177 | pid->integral = -integral_limit; |
| 178 | |
| 179 | dterm = mul_fp(pid->d_gain, (err - pid->last_err)); |
| 180 | pid->last_err = err; |
| 181 | |
| 182 | result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm; |
| 183 | |
| 184 | return (signed int)fp_toint(result); |
| 185 | } |
| 186 | |
| 187 | static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu) |
| 188 | { |
| 189 | pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct); |
| 190 | pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct); |
| 191 | pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct); |
| 192 | |
| 193 | pid_reset(&cpu->pid, |
| 194 | cpu->pstate_policy->setpoint, |
| 195 | 100, |
| 196 | cpu->pstate_policy->deadband, |
| 197 | 0); |
| 198 | } |
| 199 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 200 | static inline void intel_pstate_reset_all_pid(void) |
| 201 | { |
| 202 | unsigned int cpu; |
| 203 | for_each_online_cpu(cpu) { |
| 204 | if (all_cpu_data[cpu]) |
| 205 | intel_pstate_busy_pid_reset(all_cpu_data[cpu]); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /************************** debugfs begin ************************/ |
| 210 | static int pid_param_set(void *data, u64 val) |
| 211 | { |
| 212 | *(u32 *)data = val; |
| 213 | intel_pstate_reset_all_pid(); |
| 214 | return 0; |
| 215 | } |
| 216 | static int pid_param_get(void *data, u64 *val) |
| 217 | { |
| 218 | *val = *(u32 *)data; |
| 219 | return 0; |
| 220 | } |
| 221 | DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, |
| 222 | pid_param_set, "%llu\n"); |
| 223 | |
| 224 | struct pid_param { |
| 225 | char *name; |
| 226 | void *value; |
| 227 | }; |
| 228 | |
| 229 | static struct pid_param pid_files[] = { |
| 230 | {"sample_rate_ms", &default_policy.sample_rate_ms}, |
| 231 | {"d_gain_pct", &default_policy.d_gain_pct}, |
| 232 | {"i_gain_pct", &default_policy.i_gain_pct}, |
| 233 | {"deadband", &default_policy.deadband}, |
| 234 | {"setpoint", &default_policy.setpoint}, |
| 235 | {"p_gain_pct", &default_policy.p_gain_pct}, |
| 236 | {NULL, NULL} |
| 237 | }; |
| 238 | |
| 239 | static struct dentry *debugfs_parent; |
| 240 | static void intel_pstate_debug_expose_params(void) |
| 241 | { |
| 242 | int i = 0; |
| 243 | |
| 244 | debugfs_parent = debugfs_create_dir("pstate_snb", NULL); |
| 245 | if (IS_ERR_OR_NULL(debugfs_parent)) |
| 246 | return; |
| 247 | while (pid_files[i].name) { |
| 248 | debugfs_create_file(pid_files[i].name, 0660, |
| 249 | debugfs_parent, pid_files[i].value, |
| 250 | &fops_pid_param); |
| 251 | i++; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /************************** debugfs end ************************/ |
| 256 | |
| 257 | /************************** sysfs begin ************************/ |
| 258 | #define show_one(file_name, object) \ |
| 259 | static ssize_t show_##file_name \ |
| 260 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 261 | { \ |
| 262 | return sprintf(buf, "%u\n", limits.object); \ |
| 263 | } |
| 264 | |
| 265 | static ssize_t store_no_turbo(struct kobject *a, struct attribute *b, |
| 266 | const char *buf, size_t count) |
| 267 | { |
| 268 | unsigned int input; |
| 269 | int ret; |
| 270 | ret = sscanf(buf, "%u", &input); |
| 271 | if (ret != 1) |
| 272 | return -EINVAL; |
| 273 | limits.no_turbo = clamp_t(int, input, 0 , 1); |
| 274 | |
| 275 | return count; |
| 276 | } |
| 277 | |
| 278 | static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b, |
| 279 | const char *buf, size_t count) |
| 280 | { |
| 281 | unsigned int input; |
| 282 | int ret; |
| 283 | ret = sscanf(buf, "%u", &input); |
| 284 | if (ret != 1) |
| 285 | return -EINVAL; |
| 286 | |
Dirk Brandewie | d8f469e | 2013-05-07 08:20:26 -0700 | [diff] [blame] | 287 | limits.max_sysfs_pct = clamp_t(int, input, 0 , 100); |
| 288 | limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 289 | limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100)); |
| 290 | return count; |
| 291 | } |
| 292 | |
| 293 | static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b, |
| 294 | const char *buf, size_t count) |
| 295 | { |
| 296 | unsigned int input; |
| 297 | int ret; |
| 298 | ret = sscanf(buf, "%u", &input); |
| 299 | if (ret != 1) |
| 300 | return -EINVAL; |
| 301 | limits.min_perf_pct = clamp_t(int, input, 0 , 100); |
| 302 | limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100)); |
| 303 | |
| 304 | return count; |
| 305 | } |
| 306 | |
| 307 | show_one(no_turbo, no_turbo); |
| 308 | show_one(max_perf_pct, max_perf_pct); |
| 309 | show_one(min_perf_pct, min_perf_pct); |
| 310 | |
| 311 | define_one_global_rw(no_turbo); |
| 312 | define_one_global_rw(max_perf_pct); |
| 313 | define_one_global_rw(min_perf_pct); |
| 314 | |
| 315 | static struct attribute *intel_pstate_attributes[] = { |
| 316 | &no_turbo.attr, |
| 317 | &max_perf_pct.attr, |
| 318 | &min_perf_pct.attr, |
| 319 | NULL |
| 320 | }; |
| 321 | |
| 322 | static struct attribute_group intel_pstate_attr_group = { |
| 323 | .attrs = intel_pstate_attributes, |
| 324 | }; |
| 325 | static struct kobject *intel_pstate_kobject; |
| 326 | |
| 327 | static void intel_pstate_sysfs_expose_params(void) |
| 328 | { |
| 329 | int rc; |
| 330 | |
| 331 | intel_pstate_kobject = kobject_create_and_add("intel_pstate", |
| 332 | &cpu_subsys.dev_root->kobj); |
| 333 | BUG_ON(!intel_pstate_kobject); |
| 334 | rc = sysfs_create_group(intel_pstate_kobject, |
| 335 | &intel_pstate_attr_group); |
| 336 | BUG_ON(rc); |
| 337 | } |
| 338 | |
| 339 | /************************** sysfs end ************************/ |
| 340 | |
| 341 | static int intel_pstate_min_pstate(void) |
| 342 | { |
| 343 | u64 value; |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 344 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 345 | return (value >> 40) & 0xFF; |
| 346 | } |
| 347 | |
| 348 | static int intel_pstate_max_pstate(void) |
| 349 | { |
| 350 | u64 value; |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 351 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 352 | return (value >> 8) & 0xFF; |
| 353 | } |
| 354 | |
| 355 | static int intel_pstate_turbo_pstate(void) |
| 356 | { |
| 357 | u64 value; |
| 358 | int nont, ret; |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 359 | rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 360 | nont = intel_pstate_max_pstate(); |
| 361 | ret = ((value) & 255); |
| 362 | if (ret <= nont) |
| 363 | ret = nont; |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max) |
| 368 | { |
| 369 | int max_perf = cpu->pstate.turbo_pstate; |
| 370 | int min_perf; |
| 371 | if (limits.no_turbo) |
| 372 | max_perf = cpu->pstate.max_pstate; |
| 373 | |
| 374 | max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf)); |
| 375 | *max = clamp_t(int, max_perf, |
| 376 | cpu->pstate.min_pstate, cpu->pstate.turbo_pstate); |
| 377 | |
| 378 | min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf)); |
| 379 | *min = clamp_t(int, min_perf, |
| 380 | cpu->pstate.min_pstate, max_perf); |
| 381 | } |
| 382 | |
| 383 | static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate) |
| 384 | { |
| 385 | int max_perf, min_perf; |
| 386 | |
| 387 | intel_pstate_get_min_max(cpu, &min_perf, &max_perf); |
| 388 | |
| 389 | pstate = clamp_t(int, pstate, min_perf, max_perf); |
| 390 | |
| 391 | if (pstate == cpu->pstate.current_pstate) |
| 392 | return; |
| 393 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 394 | trace_cpu_frequency(pstate * 100000, cpu->cpu); |
Dirk Brandewie | 35363e9 | 2013-05-07 08:20:30 -0700 | [diff] [blame] | 395 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 396 | cpu->pstate.current_pstate = pstate; |
| 397 | wrmsrl(MSR_IA32_PERF_CTL, pstate << 8); |
| 398 | |
| 399 | } |
| 400 | |
| 401 | static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps) |
| 402 | { |
| 403 | int target; |
| 404 | target = cpu->pstate.current_pstate + steps; |
| 405 | |
| 406 | intel_pstate_set_pstate(cpu, target); |
| 407 | } |
| 408 | |
| 409 | static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps) |
| 410 | { |
| 411 | int target; |
| 412 | target = cpu->pstate.current_pstate - steps; |
| 413 | intel_pstate_set_pstate(cpu, target); |
| 414 | } |
| 415 | |
| 416 | static void intel_pstate_get_cpu_pstates(struct cpudata *cpu) |
| 417 | { |
| 418 | sprintf(cpu->name, "Intel 2nd generation core"); |
| 419 | |
| 420 | cpu->pstate.min_pstate = intel_pstate_min_pstate(); |
| 421 | cpu->pstate.max_pstate = intel_pstate_max_pstate(); |
| 422 | cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate(); |
| 423 | |
| 424 | /* |
| 425 | * goto max pstate so we don't slow up boot if we are built-in if we are |
| 426 | * a module we will take care of it during normal operation |
| 427 | */ |
| 428 | intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate); |
| 429 | } |
| 430 | |
| 431 | static inline void intel_pstate_calc_busy(struct cpudata *cpu, |
| 432 | struct sample *sample) |
| 433 | { |
| 434 | u64 core_pct; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 435 | core_pct = div64_u64(sample->aperf * 100, sample->mperf); |
Dirk Brandewie | e6f3eb2 | 2013-03-24 00:54:39 +0100 | [diff] [blame] | 436 | sample->freq = cpu->pstate.max_pstate * core_pct * 1000; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 437 | |
Dirk Brandewie | 1abc4b2 | 2013-05-07 08:20:25 -0700 | [diff] [blame] | 438 | sample->core_pct_busy = core_pct; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static inline void intel_pstate_sample(struct cpudata *cpu) |
| 442 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 443 | u64 aperf, mperf; |
| 444 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 445 | rdmsrl(MSR_IA32_APERF, aperf); |
| 446 | rdmsrl(MSR_IA32_MPERF, mperf); |
Dirk Brandewie | 1abc4b2 | 2013-05-07 08:20:25 -0700 | [diff] [blame] | 447 | cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT; |
| 448 | cpu->samples[cpu->sample_ptr].aperf = aperf; |
| 449 | cpu->samples[cpu->sample_ptr].mperf = mperf; |
| 450 | cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf; |
| 451 | cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 452 | |
Dirk Brandewie | 1abc4b2 | 2013-05-07 08:20:25 -0700 | [diff] [blame] | 453 | intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 454 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 455 | cpu->prev_aperf = aperf; |
| 456 | cpu->prev_mperf = mperf; |
| 457 | } |
| 458 | |
| 459 | static inline void intel_pstate_set_sample_time(struct cpudata *cpu) |
| 460 | { |
| 461 | int sample_time, delay; |
| 462 | |
| 463 | sample_time = cpu->pstate_policy->sample_rate_ms; |
| 464 | delay = msecs_to_jiffies(sample_time); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 465 | mod_timer_pinned(&cpu->timer, jiffies + delay); |
| 466 | } |
| 467 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 468 | static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu) |
| 469 | { |
| 470 | int32_t busy_scaled; |
Dirk Brandewie | 2134ed4 | 2013-07-18 08:48:42 -0700 | [diff] [blame] | 471 | int32_t core_busy, max_pstate, current_pstate; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 472 | |
| 473 | core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy); |
Dirk Brandewie | 2134ed4 | 2013-07-18 08:48:42 -0700 | [diff] [blame] | 474 | max_pstate = int_tofp(cpu->pstate.max_pstate); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 475 | current_pstate = int_tofp(cpu->pstate.current_pstate); |
Dirk Brandewie | 2134ed4 | 2013-07-18 08:48:42 -0700 | [diff] [blame] | 476 | busy_scaled = mul_fp(core_busy, div_fp(max_pstate, current_pstate)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 477 | |
| 478 | return fp_toint(busy_scaled); |
| 479 | } |
| 480 | |
| 481 | static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu) |
| 482 | { |
| 483 | int busy_scaled; |
| 484 | struct _pid *pid; |
| 485 | signed int ctl = 0; |
| 486 | int steps; |
| 487 | |
| 488 | pid = &cpu->pid; |
| 489 | busy_scaled = intel_pstate_get_scaled_busy(cpu); |
| 490 | |
| 491 | ctl = pid_calc(pid, busy_scaled); |
| 492 | |
| 493 | steps = abs(ctl); |
| 494 | if (ctl < 0) |
| 495 | intel_pstate_pstate_increase(cpu, steps); |
| 496 | else |
| 497 | intel_pstate_pstate_decrease(cpu, steps); |
| 498 | } |
| 499 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 500 | static void intel_pstate_timer_func(unsigned long __data) |
| 501 | { |
| 502 | struct cpudata *cpu = (struct cpudata *) __data; |
| 503 | |
| 504 | intel_pstate_sample(cpu); |
Dirk Brandewie | ca182ae | 2013-05-07 08:20:27 -0700 | [diff] [blame] | 505 | intel_pstate_adjust_busy_pstate(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 506 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 507 | if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) { |
| 508 | cpu->min_pstate_count++; |
| 509 | if (!(cpu->min_pstate_count % 5)) { |
| 510 | intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 511 | } |
| 512 | } else |
| 513 | cpu->min_pstate_count = 0; |
Dirk Brandewie | ca182ae | 2013-05-07 08:20:27 -0700 | [diff] [blame] | 514 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 515 | intel_pstate_set_sample_time(cpu); |
| 516 | } |
| 517 | |
| 518 | #define ICPU(model, policy) \ |
| 519 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy } |
| 520 | |
| 521 | static const struct x86_cpu_id intel_pstate_cpu_ids[] = { |
| 522 | ICPU(0x2a, default_policy), |
| 523 | ICPU(0x2d, default_policy), |
Dirk Brandewie | c96d53d | 2013-05-17 16:10:24 +0000 | [diff] [blame] | 524 | ICPU(0x3a, default_policy), |
Nell Hardcastle | 6cdcdb7 | 2013-06-30 15:58:57 -0700 | [diff] [blame] | 525 | ICPU(0x3c, default_policy), |
| 526 | ICPU(0x3e, default_policy), |
| 527 | ICPU(0x3f, default_policy), |
| 528 | ICPU(0x45, default_policy), |
| 529 | ICPU(0x46, default_policy), |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 530 | {} |
| 531 | }; |
| 532 | MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids); |
| 533 | |
| 534 | static int intel_pstate_init_cpu(unsigned int cpunum) |
| 535 | { |
| 536 | |
| 537 | const struct x86_cpu_id *id; |
| 538 | struct cpudata *cpu; |
| 539 | |
| 540 | id = x86_match_cpu(intel_pstate_cpu_ids); |
| 541 | if (!id) |
| 542 | return -ENODEV; |
| 543 | |
| 544 | all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL); |
| 545 | if (!all_cpu_data[cpunum]) |
| 546 | return -ENOMEM; |
| 547 | |
| 548 | cpu = all_cpu_data[cpunum]; |
| 549 | |
| 550 | intel_pstate_get_cpu_pstates(cpu); |
| 551 | |
| 552 | cpu->cpu = cpunum; |
| 553 | cpu->pstate_policy = |
| 554 | (struct pstate_adjust_policy *)id->driver_data; |
| 555 | init_timer_deferrable(&cpu->timer); |
| 556 | cpu->timer.function = intel_pstate_timer_func; |
| 557 | cpu->timer.data = |
| 558 | (unsigned long)cpu; |
| 559 | cpu->timer.expires = jiffies + HZ/100; |
| 560 | intel_pstate_busy_pid_reset(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 561 | intel_pstate_sample(cpu); |
| 562 | intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate); |
| 563 | |
| 564 | add_timer_on(&cpu->timer, cpunum); |
| 565 | |
| 566 | pr_info("Intel pstate controlling: cpu %d\n", cpunum); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static unsigned int intel_pstate_get(unsigned int cpu_num) |
| 572 | { |
| 573 | struct sample *sample; |
| 574 | struct cpudata *cpu; |
| 575 | |
| 576 | cpu = all_cpu_data[cpu_num]; |
| 577 | if (!cpu) |
| 578 | return 0; |
| 579 | sample = &cpu->samples[cpu->sample_ptr]; |
| 580 | return sample->freq; |
| 581 | } |
| 582 | |
| 583 | static int intel_pstate_set_policy(struct cpufreq_policy *policy) |
| 584 | { |
| 585 | struct cpudata *cpu; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 586 | |
| 587 | cpu = all_cpu_data[policy->cpu]; |
| 588 | |
Dirk Brandewie | d3929b8 | 2013-03-05 14:15:26 -0800 | [diff] [blame] | 589 | if (!policy->cpuinfo.max_freq) |
| 590 | return -ENODEV; |
| 591 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 592 | if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) { |
| 593 | limits.min_perf_pct = 100; |
| 594 | limits.min_perf = int_tofp(1); |
| 595 | limits.max_perf_pct = 100; |
| 596 | limits.max_perf = int_tofp(1); |
| 597 | limits.no_turbo = 0; |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 598 | return 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 599 | } |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 600 | limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq; |
| 601 | limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100); |
| 602 | limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100)); |
| 603 | |
Dirk Brandewie | d8f469e | 2013-05-07 08:20:26 -0700 | [diff] [blame] | 604 | limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq; |
| 605 | limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100); |
| 606 | limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct); |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 607 | limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int intel_pstate_verify_policy(struct cpufreq_policy *policy) |
| 613 | { |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 614 | cpufreq_verify_within_cpu_limits(policy); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 615 | |
| 616 | if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) && |
| 617 | (policy->policy != CPUFREQ_POLICY_PERFORMANCE)) |
| 618 | return -EINVAL; |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
Paul Gortmaker | 2760984 | 2013-06-19 13:54:04 -0400 | [diff] [blame] | 623 | static int intel_pstate_cpu_exit(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 624 | { |
| 625 | int cpu = policy->cpu; |
| 626 | |
| 627 | del_timer(&all_cpu_data[cpu]->timer); |
| 628 | kfree(all_cpu_data[cpu]); |
| 629 | all_cpu_data[cpu] = NULL; |
| 630 | return 0; |
| 631 | } |
| 632 | |
Paul Gortmaker | 2760984 | 2013-06-19 13:54:04 -0400 | [diff] [blame] | 633 | static int intel_pstate_cpu_init(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 634 | { |
| 635 | int rc, min_pstate, max_pstate; |
| 636 | struct cpudata *cpu; |
| 637 | |
| 638 | rc = intel_pstate_init_cpu(policy->cpu); |
| 639 | if (rc) |
| 640 | return rc; |
| 641 | |
| 642 | cpu = all_cpu_data[policy->cpu]; |
| 643 | |
| 644 | if (!limits.no_turbo && |
| 645 | limits.min_perf_pct == 100 && limits.max_perf_pct == 100) |
| 646 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
| 647 | else |
| 648 | policy->policy = CPUFREQ_POLICY_POWERSAVE; |
| 649 | |
| 650 | intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate); |
| 651 | policy->min = min_pstate * 100000; |
| 652 | policy->max = max_pstate * 100000; |
| 653 | |
| 654 | /* cpuinfo and default policy values */ |
| 655 | policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000; |
| 656 | policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000; |
| 657 | policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; |
| 658 | cpumask_set_cpu(policy->cpu, policy->cpus); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static struct cpufreq_driver intel_pstate_driver = { |
| 664 | .flags = CPUFREQ_CONST_LOOPS, |
| 665 | .verify = intel_pstate_verify_policy, |
| 666 | .setpolicy = intel_pstate_set_policy, |
| 667 | .get = intel_pstate_get, |
| 668 | .init = intel_pstate_cpu_init, |
| 669 | .exit = intel_pstate_cpu_exit, |
| 670 | .name = "intel_pstate", |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 671 | }; |
| 672 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 673 | static int __initdata no_load; |
| 674 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 675 | static int intel_pstate_msrs_not_valid(void) |
| 676 | { |
| 677 | /* Check that all the msr's we are using are valid. */ |
| 678 | u64 aperf, mperf, tmp; |
| 679 | |
| 680 | rdmsrl(MSR_IA32_APERF, aperf); |
| 681 | rdmsrl(MSR_IA32_MPERF, mperf); |
| 682 | |
| 683 | if (!intel_pstate_min_pstate() || |
| 684 | !intel_pstate_max_pstate() || |
| 685 | !intel_pstate_turbo_pstate()) |
| 686 | return -ENODEV; |
| 687 | |
| 688 | rdmsrl(MSR_IA32_APERF, tmp); |
| 689 | if (!(tmp - aperf)) |
| 690 | return -ENODEV; |
| 691 | |
| 692 | rdmsrl(MSR_IA32_MPERF, tmp); |
| 693 | if (!(tmp - mperf)) |
| 694 | return -ENODEV; |
| 695 | |
| 696 | return 0; |
| 697 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 698 | static int __init intel_pstate_init(void) |
| 699 | { |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 700 | int cpu, rc = 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 701 | const struct x86_cpu_id *id; |
| 702 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 703 | if (no_load) |
| 704 | return -ENODEV; |
| 705 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 706 | id = x86_match_cpu(intel_pstate_cpu_ids); |
| 707 | if (!id) |
| 708 | return -ENODEV; |
| 709 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 710 | if (intel_pstate_msrs_not_valid()) |
| 711 | return -ENODEV; |
| 712 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 713 | pr_info("Intel P-state driver initializing.\n"); |
| 714 | |
Wei Yongjun | b57ffac | 2013-05-13 08:03:43 +0000 | [diff] [blame] | 715 | all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus()); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 716 | if (!all_cpu_data) |
| 717 | return -ENOMEM; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 718 | |
| 719 | rc = cpufreq_register_driver(&intel_pstate_driver); |
| 720 | if (rc) |
| 721 | goto out; |
| 722 | |
| 723 | intel_pstate_debug_expose_params(); |
| 724 | intel_pstate_sysfs_expose_params(); |
| 725 | return rc; |
| 726 | out: |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 727 | get_online_cpus(); |
| 728 | for_each_online_cpu(cpu) { |
| 729 | if (all_cpu_data[cpu]) { |
| 730 | del_timer_sync(&all_cpu_data[cpu]->timer); |
| 731 | kfree(all_cpu_data[cpu]); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | put_online_cpus(); |
| 736 | vfree(all_cpu_data); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 737 | return -ENODEV; |
| 738 | } |
| 739 | device_initcall(intel_pstate_init); |
| 740 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 741 | static int __init intel_pstate_setup(char *str) |
| 742 | { |
| 743 | if (!str) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | if (!strcmp(str, "disable")) |
| 747 | no_load = 1; |
| 748 | return 0; |
| 749 | } |
| 750 | early_param("intel_pstate", intel_pstate_setup); |
| 751 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 752 | MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>"); |
| 753 | MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors"); |
| 754 | MODULE_LICENSE("GPL"); |