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