intel_pstate.c 26 KB

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