intel_pstate.c 29 KB

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