intel_pstate.c 29 KB

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