intel_pstate.c 23 KB

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