intel_pstate.c 29 KB

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