intel_pstate.c 24 KB

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