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