intel_pstate.c 27 KB

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