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