intel_pstate.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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 <linux/vmalloc.h>
  29. #include <trace/events/power.h>
  30. #include <asm/div64.h>
  31. #include <asm/msr.h>
  32. #include <asm/cpu_device_id.h>
  33. #include <asm/cpufeature.h>
  34. #define ATOM_RATIOS 0x66a
  35. #define ATOM_VIDS 0x66b
  36. #define ATOM_TURBO_RATIOS 0x66c
  37. #define ATOM_TURBO_VIDS 0x66d
  38. #define FRAC_BITS 8
  39. #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  40. #define fp_toint(X) ((X) >> FRAC_BITS)
  41. static inline int32_t mul_fp(int32_t x, int32_t y)
  42. {
  43. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  44. }
  45. static inline int32_t div_fp(s64 x, s64 y)
  46. {
  47. return div64_s64((int64_t)x << FRAC_BITS, y);
  48. }
  49. static inline int ceiling_fp(int32_t x)
  50. {
  51. int mask, ret;
  52. ret = fp_toint(x);
  53. mask = (1 << FRAC_BITS) - 1;
  54. if (x & mask)
  55. ret += 1;
  56. return ret;
  57. }
  58. /**
  59. * struct sample - Store performance sample
  60. * @core_pct_busy: Ratio of APERF/MPERF in percent, which is actual
  61. * performance during last sample period
  62. * @busy_scaled: Scaled busy value which is used to calculate next
  63. * P state. This can be different than core_pct_busy
  64. * to account for cpu idle period
  65. * @aperf: Difference of actual performance frequency clock count
  66. * read from APERF MSR between last and current sample
  67. * @mperf: Difference of maximum performance frequency clock count
  68. * read from MPERF MSR between last and current sample
  69. * @tsc: Difference of time stamp counter between last and
  70. * current sample
  71. * @freq: Effective frequency calculated from APERF/MPERF
  72. * @time: Current time from scheduler
  73. *
  74. * This structure is used in the cpudata structure to store performance sample
  75. * data for choosing next P State.
  76. */
  77. struct sample {
  78. int32_t core_pct_busy;
  79. int32_t busy_scaled;
  80. u64 aperf;
  81. u64 mperf;
  82. u64 tsc;
  83. int freq;
  84. u64 time;
  85. };
  86. /**
  87. * struct pstate_data - Store P state data
  88. * @current_pstate: Current requested P state
  89. * @min_pstate: Min P state possible for this platform
  90. * @max_pstate: Max P state possible for this platform
  91. * @max_pstate_physical:This is physical Max P state for a processor
  92. * This can be higher than the max_pstate which can
  93. * be limited by platform thermal design power limits
  94. * @scaling: Scaling factor to convert frequency to cpufreq
  95. * frequency units
  96. * @turbo_pstate: Max Turbo P state possible for this platform
  97. *
  98. * Stores the per cpu model P state limits and current P state.
  99. */
  100. struct pstate_data {
  101. int current_pstate;
  102. int min_pstate;
  103. int max_pstate;
  104. int max_pstate_physical;
  105. int scaling;
  106. int turbo_pstate;
  107. };
  108. /**
  109. * struct vid_data - Stores voltage information data
  110. * @min: VID data for this platform corresponding to
  111. * the lowest P state
  112. * @max: VID data corresponding to the highest P State.
  113. * @turbo: VID data for turbo P state
  114. * @ratio: Ratio of (vid max - vid min) /
  115. * (max P state - Min P State)
  116. *
  117. * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
  118. * This data is used in Atom platforms, where in addition to target P state,
  119. * the voltage data needs to be specified to select next P State.
  120. */
  121. struct vid_data {
  122. int min;
  123. int max;
  124. int turbo;
  125. int32_t ratio;
  126. };
  127. /**
  128. * struct _pid - Stores PID data
  129. * @setpoint: Target set point for busyness or performance
  130. * @integral: Storage for accumulated error values
  131. * @p_gain: PID proportional gain
  132. * @i_gain: PID integral gain
  133. * @d_gain: PID derivative gain
  134. * @deadband: PID deadband
  135. * @last_err: Last error storage for integral part of PID calculation
  136. *
  137. * Stores PID coefficients and last error for PID controller.
  138. */
  139. struct _pid {
  140. int setpoint;
  141. int32_t integral;
  142. int32_t p_gain;
  143. int32_t i_gain;
  144. int32_t d_gain;
  145. int deadband;
  146. int32_t last_err;
  147. };
  148. /**
  149. * struct cpudata - Per CPU instance data storage
  150. * @cpu: CPU number for this instance data
  151. * @update_util: CPUFreq utility callback information
  152. * @pstate: Stores P state limits for this CPU
  153. * @vid: Stores VID limits for this CPU
  154. * @pid: Stores PID parameters for this CPU
  155. * @last_sample_time: Last Sample time
  156. * @prev_aperf: Last APERF value read from APERF MSR
  157. * @prev_mperf: Last MPERF value read from MPERF MSR
  158. * @prev_tsc: Last timestamp counter (TSC) value
  159. * @prev_cummulative_iowait: IO Wait time difference from last and
  160. * current sample
  161. * @sample: Storage for storing last Sample data
  162. *
  163. * This structure stores per CPU instance data for all CPUs.
  164. */
  165. struct cpudata {
  166. int cpu;
  167. struct update_util_data update_util;
  168. struct pstate_data pstate;
  169. struct vid_data vid;
  170. struct _pid pid;
  171. u64 last_sample_time;
  172. u64 prev_aperf;
  173. u64 prev_mperf;
  174. u64 prev_tsc;
  175. u64 prev_cummulative_iowait;
  176. struct sample sample;
  177. };
  178. static struct cpudata **all_cpu_data;
  179. /**
  180. * struct pid_adjust_policy - Stores static PID configuration data
  181. * @sample_rate_ms: PID calculation sample rate in ms
  182. * @sample_rate_ns: Sample rate calculation in ns
  183. * @deadband: PID deadband
  184. * @setpoint: PID Setpoint
  185. * @p_gain_pct: PID proportional gain
  186. * @i_gain_pct: PID integral gain
  187. * @d_gain_pct: PID derivative gain
  188. *
  189. * Stores per CPU model static PID configuration data.
  190. */
  191. struct pstate_adjust_policy {
  192. int sample_rate_ms;
  193. s64 sample_rate_ns;
  194. int deadband;
  195. int setpoint;
  196. int p_gain_pct;
  197. int d_gain_pct;
  198. int i_gain_pct;
  199. };
  200. /**
  201. * struct pstate_funcs - Per CPU model specific callbacks
  202. * @get_max: Callback to get maximum non turbo effective P state
  203. * @get_max_physical: Callback to get maximum non turbo physical P state
  204. * @get_min: Callback to get minimum P state
  205. * @get_turbo: Callback to get turbo P state
  206. * @get_scaling: Callback to get frequency scaling factor
  207. * @get_val: Callback to convert P state to actual MSR write value
  208. * @get_vid: Callback to get VID data for Atom platforms
  209. * @get_target_pstate: Callback to a function to calculate next P state to use
  210. *
  211. * Core and Atom CPU models have different way to get P State limits. This
  212. * structure is used to store those callbacks.
  213. */
  214. struct pstate_funcs {
  215. int (*get_max)(void);
  216. int (*get_max_physical)(void);
  217. int (*get_min)(void);
  218. int (*get_turbo)(void);
  219. int (*get_scaling)(void);
  220. u64 (*get_val)(struct cpudata*, int pstate);
  221. void (*get_vid)(struct cpudata *);
  222. int32_t (*get_target_pstate)(struct cpudata *);
  223. };
  224. /**
  225. * struct cpu_defaults- Per CPU model default config data
  226. * @pid_policy: PID config data
  227. * @funcs: Callback function data
  228. */
  229. struct cpu_defaults {
  230. struct pstate_adjust_policy pid_policy;
  231. struct pstate_funcs funcs;
  232. };
  233. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
  234. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
  235. static struct pstate_adjust_policy pid_params;
  236. static struct pstate_funcs pstate_funcs;
  237. static int hwp_active;
  238. /**
  239. * struct perf_limits - Store user and policy limits
  240. * @no_turbo: User requested turbo state from intel_pstate sysfs
  241. * @turbo_disabled: Platform turbo status either from msr
  242. * MSR_IA32_MISC_ENABLE or when maximum available pstate
  243. * matches the maximum turbo pstate
  244. * @max_perf_pct: Effective maximum performance limit in percentage, this
  245. * is minimum of either limits enforced by cpufreq policy
  246. * or limits from user set limits via intel_pstate sysfs
  247. * @min_perf_pct: Effective minimum performance limit in percentage, this
  248. * is maximum of either limits enforced by cpufreq policy
  249. * or limits from user set limits via intel_pstate sysfs
  250. * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
  251. * This value is used to limit max pstate
  252. * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
  253. * This value is used to limit min pstate
  254. * @max_policy_pct: The maximum performance in percentage enforced by
  255. * cpufreq setpolicy interface
  256. * @max_sysfs_pct: The maximum performance in percentage enforced by
  257. * intel pstate sysfs interface
  258. * @min_policy_pct: The minimum performance in percentage enforced by
  259. * cpufreq setpolicy interface
  260. * @min_sysfs_pct: The minimum performance in percentage enforced by
  261. * intel pstate sysfs interface
  262. *
  263. * Storage for user and policy defined limits.
  264. */
  265. struct perf_limits {
  266. int no_turbo;
  267. int turbo_disabled;
  268. int max_perf_pct;
  269. int min_perf_pct;
  270. int32_t max_perf;
  271. int32_t min_perf;
  272. int max_policy_pct;
  273. int max_sysfs_pct;
  274. int min_policy_pct;
  275. int min_sysfs_pct;
  276. };
  277. static struct perf_limits performance_limits = {
  278. .no_turbo = 0,
  279. .turbo_disabled = 0,
  280. .max_perf_pct = 100,
  281. .max_perf = int_tofp(1),
  282. .min_perf_pct = 100,
  283. .min_perf = int_tofp(1),
  284. .max_policy_pct = 100,
  285. .max_sysfs_pct = 100,
  286. .min_policy_pct = 0,
  287. .min_sysfs_pct = 0,
  288. };
  289. static struct perf_limits powersave_limits = {
  290. .no_turbo = 0,
  291. .turbo_disabled = 0,
  292. .max_perf_pct = 100,
  293. .max_perf = int_tofp(1),
  294. .min_perf_pct = 0,
  295. .min_perf = 0,
  296. .max_policy_pct = 100,
  297. .max_sysfs_pct = 100,
  298. .min_policy_pct = 0,
  299. .min_sysfs_pct = 0,
  300. };
  301. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  302. static struct perf_limits *limits = &performance_limits;
  303. #else
  304. static struct perf_limits *limits = &powersave_limits;
  305. #endif
  306. static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
  307. int deadband, int integral) {
  308. pid->setpoint = int_tofp(setpoint);
  309. pid->deadband = int_tofp(deadband);
  310. pid->integral = int_tofp(integral);
  311. pid->last_err = int_tofp(setpoint) - int_tofp(busy);
  312. }
  313. static inline void pid_p_gain_set(struct _pid *pid, int percent)
  314. {
  315. pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
  316. }
  317. static inline void pid_i_gain_set(struct _pid *pid, int percent)
  318. {
  319. pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
  320. }
  321. static inline void pid_d_gain_set(struct _pid *pid, int percent)
  322. {
  323. pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
  324. }
  325. static signed int pid_calc(struct _pid *pid, int32_t busy)
  326. {
  327. signed int result;
  328. int32_t pterm, dterm, fp_error;
  329. int32_t integral_limit;
  330. fp_error = pid->setpoint - busy;
  331. if (abs(fp_error) <= pid->deadband)
  332. return 0;
  333. pterm = mul_fp(pid->p_gain, fp_error);
  334. pid->integral += fp_error;
  335. /*
  336. * We limit the integral here so that it will never
  337. * get higher than 30. This prevents it from becoming
  338. * too large an input over long periods of time and allows
  339. * it to get factored out sooner.
  340. *
  341. * The value of 30 was chosen through experimentation.
  342. */
  343. integral_limit = int_tofp(30);
  344. if (pid->integral > integral_limit)
  345. pid->integral = integral_limit;
  346. if (pid->integral < -integral_limit)
  347. pid->integral = -integral_limit;
  348. dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
  349. pid->last_err = fp_error;
  350. result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
  351. result = result + (1 << (FRAC_BITS-1));
  352. return (signed int)fp_toint(result);
  353. }
  354. static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
  355. {
  356. pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
  357. pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
  358. pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
  359. pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
  360. }
  361. static inline void intel_pstate_reset_all_pid(void)
  362. {
  363. unsigned int cpu;
  364. for_each_online_cpu(cpu) {
  365. if (all_cpu_data[cpu])
  366. intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
  367. }
  368. }
  369. static inline void update_turbo_state(void)
  370. {
  371. u64 misc_en;
  372. struct cpudata *cpu;
  373. cpu = all_cpu_data[0];
  374. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  375. limits->turbo_disabled =
  376. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  377. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  378. }
  379. static void intel_pstate_hwp_set(const struct cpumask *cpumask)
  380. {
  381. int min, hw_min, max, hw_max, cpu, range, adj_range;
  382. u64 value, cap;
  383. rdmsrl(MSR_HWP_CAPABILITIES, cap);
  384. hw_min = HWP_LOWEST_PERF(cap);
  385. hw_max = HWP_HIGHEST_PERF(cap);
  386. range = hw_max - hw_min;
  387. for_each_cpu(cpu, cpumask) {
  388. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  389. adj_range = limits->min_perf_pct * range / 100;
  390. min = hw_min + adj_range;
  391. value &= ~HWP_MIN_PERF(~0L);
  392. value |= HWP_MIN_PERF(min);
  393. adj_range = limits->max_perf_pct * range / 100;
  394. max = hw_min + adj_range;
  395. if (limits->no_turbo) {
  396. hw_max = HWP_GUARANTEED_PERF(cap);
  397. if (hw_max < max)
  398. max = hw_max;
  399. }
  400. value &= ~HWP_MAX_PERF(~0L);
  401. value |= HWP_MAX_PERF(max);
  402. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  403. }
  404. }
  405. static void intel_pstate_hwp_set_online_cpus(void)
  406. {
  407. get_online_cpus();
  408. intel_pstate_hwp_set(cpu_online_mask);
  409. put_online_cpus();
  410. }
  411. /************************** debugfs begin ************************/
  412. static int pid_param_set(void *data, u64 val)
  413. {
  414. *(u32 *)data = val;
  415. intel_pstate_reset_all_pid();
  416. return 0;
  417. }
  418. static int pid_param_get(void *data, u64 *val)
  419. {
  420. *val = *(u32 *)data;
  421. return 0;
  422. }
  423. DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
  424. struct pid_param {
  425. char *name;
  426. void *value;
  427. };
  428. static struct pid_param pid_files[] = {
  429. {"sample_rate_ms", &pid_params.sample_rate_ms},
  430. {"d_gain_pct", &pid_params.d_gain_pct},
  431. {"i_gain_pct", &pid_params.i_gain_pct},
  432. {"deadband", &pid_params.deadband},
  433. {"setpoint", &pid_params.setpoint},
  434. {"p_gain_pct", &pid_params.p_gain_pct},
  435. {NULL, NULL}
  436. };
  437. static void __init intel_pstate_debug_expose_params(void)
  438. {
  439. struct dentry *debugfs_parent;
  440. int i = 0;
  441. if (hwp_active)
  442. return;
  443. debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
  444. if (IS_ERR_OR_NULL(debugfs_parent))
  445. return;
  446. while (pid_files[i].name) {
  447. debugfs_create_file(pid_files[i].name, 0660,
  448. debugfs_parent, pid_files[i].value,
  449. &fops_pid_param);
  450. i++;
  451. }
  452. }
  453. /************************** debugfs end ************************/
  454. /************************** sysfs begin ************************/
  455. #define show_one(file_name, object) \
  456. static ssize_t show_##file_name \
  457. (struct kobject *kobj, struct attribute *attr, char *buf) \
  458. { \
  459. return sprintf(buf, "%u\n", limits->object); \
  460. }
  461. static ssize_t show_turbo_pct(struct kobject *kobj,
  462. struct attribute *attr, char *buf)
  463. {
  464. struct cpudata *cpu;
  465. int total, no_turbo, turbo_pct;
  466. uint32_t turbo_fp;
  467. cpu = all_cpu_data[0];
  468. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  469. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  470. turbo_fp = div_fp(int_tofp(no_turbo), int_tofp(total));
  471. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  472. return sprintf(buf, "%u\n", turbo_pct);
  473. }
  474. static ssize_t show_num_pstates(struct kobject *kobj,
  475. struct attribute *attr, char *buf)
  476. {
  477. struct cpudata *cpu;
  478. int total;
  479. cpu = all_cpu_data[0];
  480. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  481. return sprintf(buf, "%u\n", total);
  482. }
  483. static ssize_t show_no_turbo(struct kobject *kobj,
  484. struct attribute *attr, char *buf)
  485. {
  486. ssize_t ret;
  487. update_turbo_state();
  488. if (limits->turbo_disabled)
  489. ret = sprintf(buf, "%u\n", limits->turbo_disabled);
  490. else
  491. ret = sprintf(buf, "%u\n", limits->no_turbo);
  492. return ret;
  493. }
  494. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  495. const char *buf, size_t count)
  496. {
  497. unsigned int input;
  498. int ret;
  499. ret = sscanf(buf, "%u", &input);
  500. if (ret != 1)
  501. return -EINVAL;
  502. update_turbo_state();
  503. if (limits->turbo_disabled) {
  504. pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n");
  505. return -EPERM;
  506. }
  507. limits->no_turbo = clamp_t(int, input, 0, 1);
  508. if (hwp_active)
  509. intel_pstate_hwp_set_online_cpus();
  510. return count;
  511. }
  512. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  513. const char *buf, size_t count)
  514. {
  515. unsigned int input;
  516. int ret;
  517. ret = sscanf(buf, "%u", &input);
  518. if (ret != 1)
  519. return -EINVAL;
  520. limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
  521. limits->max_perf_pct = min(limits->max_policy_pct,
  522. limits->max_sysfs_pct);
  523. limits->max_perf_pct = max(limits->min_policy_pct,
  524. limits->max_perf_pct);
  525. limits->max_perf_pct = max(limits->min_perf_pct,
  526. limits->max_perf_pct);
  527. limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
  528. int_tofp(100));
  529. if (hwp_active)
  530. intel_pstate_hwp_set_online_cpus();
  531. return count;
  532. }
  533. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  534. const char *buf, size_t count)
  535. {
  536. unsigned int input;
  537. int ret;
  538. ret = sscanf(buf, "%u", &input);
  539. if (ret != 1)
  540. return -EINVAL;
  541. limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
  542. limits->min_perf_pct = max(limits->min_policy_pct,
  543. limits->min_sysfs_pct);
  544. limits->min_perf_pct = min(limits->max_policy_pct,
  545. limits->min_perf_pct);
  546. limits->min_perf_pct = min(limits->max_perf_pct,
  547. limits->min_perf_pct);
  548. limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
  549. int_tofp(100));
  550. if (hwp_active)
  551. intel_pstate_hwp_set_online_cpus();
  552. return count;
  553. }
  554. show_one(max_perf_pct, max_perf_pct);
  555. show_one(min_perf_pct, min_perf_pct);
  556. define_one_global_rw(no_turbo);
  557. define_one_global_rw(max_perf_pct);
  558. define_one_global_rw(min_perf_pct);
  559. define_one_global_ro(turbo_pct);
  560. define_one_global_ro(num_pstates);
  561. static struct attribute *intel_pstate_attributes[] = {
  562. &no_turbo.attr,
  563. &max_perf_pct.attr,
  564. &min_perf_pct.attr,
  565. &turbo_pct.attr,
  566. &num_pstates.attr,
  567. NULL
  568. };
  569. static struct attribute_group intel_pstate_attr_group = {
  570. .attrs = intel_pstate_attributes,
  571. };
  572. static void __init intel_pstate_sysfs_expose_params(void)
  573. {
  574. struct kobject *intel_pstate_kobject;
  575. int rc;
  576. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  577. &cpu_subsys.dev_root->kobj);
  578. BUG_ON(!intel_pstate_kobject);
  579. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  580. BUG_ON(rc);
  581. }
  582. /************************** sysfs end ************************/
  583. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  584. {
  585. /* First disable HWP notification interrupt as we don't process them */
  586. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  587. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  588. }
  589. static int atom_get_min_pstate(void)
  590. {
  591. u64 value;
  592. rdmsrl(ATOM_RATIOS, value);
  593. return (value >> 8) & 0x7F;
  594. }
  595. static int atom_get_max_pstate(void)
  596. {
  597. u64 value;
  598. rdmsrl(ATOM_RATIOS, value);
  599. return (value >> 16) & 0x7F;
  600. }
  601. static int atom_get_turbo_pstate(void)
  602. {
  603. u64 value;
  604. rdmsrl(ATOM_TURBO_RATIOS, value);
  605. return value & 0x7F;
  606. }
  607. static u64 atom_get_val(struct cpudata *cpudata, int pstate)
  608. {
  609. u64 val;
  610. int32_t vid_fp;
  611. u32 vid;
  612. val = (u64)pstate << 8;
  613. if (limits->no_turbo && !limits->turbo_disabled)
  614. val |= (u64)1 << 32;
  615. vid_fp = cpudata->vid.min + mul_fp(
  616. int_tofp(pstate - cpudata->pstate.min_pstate),
  617. cpudata->vid.ratio);
  618. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  619. vid = ceiling_fp(vid_fp);
  620. if (pstate > cpudata->pstate.max_pstate)
  621. vid = cpudata->vid.turbo;
  622. return val | vid;
  623. }
  624. static int silvermont_get_scaling(void)
  625. {
  626. u64 value;
  627. int i;
  628. /* Defined in Table 35-6 from SDM (Sept 2015) */
  629. static int silvermont_freq_table[] = {
  630. 83300, 100000, 133300, 116700, 80000};
  631. rdmsrl(MSR_FSB_FREQ, value);
  632. i = value & 0x7;
  633. WARN_ON(i > 4);
  634. return silvermont_freq_table[i];
  635. }
  636. static int airmont_get_scaling(void)
  637. {
  638. u64 value;
  639. int i;
  640. /* Defined in Table 35-10 from SDM (Sept 2015) */
  641. static int airmont_freq_table[] = {
  642. 83300, 100000, 133300, 116700, 80000,
  643. 93300, 90000, 88900, 87500};
  644. rdmsrl(MSR_FSB_FREQ, value);
  645. i = value & 0xF;
  646. WARN_ON(i > 8);
  647. return airmont_freq_table[i];
  648. }
  649. static void atom_get_vid(struct cpudata *cpudata)
  650. {
  651. u64 value;
  652. rdmsrl(ATOM_VIDS, value);
  653. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  654. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  655. cpudata->vid.ratio = div_fp(
  656. cpudata->vid.max - cpudata->vid.min,
  657. int_tofp(cpudata->pstate.max_pstate -
  658. cpudata->pstate.min_pstate));
  659. rdmsrl(ATOM_TURBO_VIDS, value);
  660. cpudata->vid.turbo = value & 0x7f;
  661. }
  662. static int core_get_min_pstate(void)
  663. {
  664. u64 value;
  665. rdmsrl(MSR_PLATFORM_INFO, value);
  666. return (value >> 40) & 0xFF;
  667. }
  668. static int core_get_max_pstate_physical(void)
  669. {
  670. u64 value;
  671. rdmsrl(MSR_PLATFORM_INFO, value);
  672. return (value >> 8) & 0xFF;
  673. }
  674. static int core_get_max_pstate(void)
  675. {
  676. u64 tar;
  677. u64 plat_info;
  678. int max_pstate;
  679. int err;
  680. rdmsrl(MSR_PLATFORM_INFO, plat_info);
  681. max_pstate = (plat_info >> 8) & 0xFF;
  682. err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
  683. if (!err) {
  684. /* Do some sanity checking for safety */
  685. if (plat_info & 0x600000000) {
  686. u64 tdp_ctrl;
  687. u64 tdp_ratio;
  688. int tdp_msr;
  689. err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  690. if (err)
  691. goto skip_tar;
  692. tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
  693. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  694. if (err)
  695. goto skip_tar;
  696. if (tdp_ratio - 1 == tar) {
  697. max_pstate = tar;
  698. pr_debug("max_pstate=TAC %x\n", max_pstate);
  699. } else {
  700. goto skip_tar;
  701. }
  702. }
  703. }
  704. skip_tar:
  705. return max_pstate;
  706. }
  707. static int core_get_turbo_pstate(void)
  708. {
  709. u64 value;
  710. int nont, ret;
  711. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  712. nont = core_get_max_pstate();
  713. ret = (value) & 255;
  714. if (ret <= nont)
  715. ret = nont;
  716. return ret;
  717. }
  718. static inline int core_get_scaling(void)
  719. {
  720. return 100000;
  721. }
  722. static u64 core_get_val(struct cpudata *cpudata, int pstate)
  723. {
  724. u64 val;
  725. val = (u64)pstate << 8;
  726. if (limits->no_turbo && !limits->turbo_disabled)
  727. val |= (u64)1 << 32;
  728. return val;
  729. }
  730. static int knl_get_turbo_pstate(void)
  731. {
  732. u64 value;
  733. int nont, ret;
  734. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  735. nont = core_get_max_pstate();
  736. ret = (((value) >> 8) & 0xFF);
  737. if (ret <= nont)
  738. ret = nont;
  739. return ret;
  740. }
  741. static struct cpu_defaults core_params = {
  742. .pid_policy = {
  743. .sample_rate_ms = 10,
  744. .deadband = 0,
  745. .setpoint = 97,
  746. .p_gain_pct = 20,
  747. .d_gain_pct = 0,
  748. .i_gain_pct = 0,
  749. },
  750. .funcs = {
  751. .get_max = core_get_max_pstate,
  752. .get_max_physical = core_get_max_pstate_physical,
  753. .get_min = core_get_min_pstate,
  754. .get_turbo = core_get_turbo_pstate,
  755. .get_scaling = core_get_scaling,
  756. .get_val = core_get_val,
  757. .get_target_pstate = get_target_pstate_use_performance,
  758. },
  759. };
  760. static struct cpu_defaults silvermont_params = {
  761. .pid_policy = {
  762. .sample_rate_ms = 10,
  763. .deadband = 0,
  764. .setpoint = 60,
  765. .p_gain_pct = 14,
  766. .d_gain_pct = 0,
  767. .i_gain_pct = 4,
  768. },
  769. .funcs = {
  770. .get_max = atom_get_max_pstate,
  771. .get_max_physical = atom_get_max_pstate,
  772. .get_min = atom_get_min_pstate,
  773. .get_turbo = atom_get_turbo_pstate,
  774. .get_val = atom_get_val,
  775. .get_scaling = silvermont_get_scaling,
  776. .get_vid = atom_get_vid,
  777. .get_target_pstate = get_target_pstate_use_cpu_load,
  778. },
  779. };
  780. static struct cpu_defaults airmont_params = {
  781. .pid_policy = {
  782. .sample_rate_ms = 10,
  783. .deadband = 0,
  784. .setpoint = 60,
  785. .p_gain_pct = 14,
  786. .d_gain_pct = 0,
  787. .i_gain_pct = 4,
  788. },
  789. .funcs = {
  790. .get_max = atom_get_max_pstate,
  791. .get_max_physical = atom_get_max_pstate,
  792. .get_min = atom_get_min_pstate,
  793. .get_turbo = atom_get_turbo_pstate,
  794. .get_val = atom_get_val,
  795. .get_scaling = airmont_get_scaling,
  796. .get_vid = atom_get_vid,
  797. .get_target_pstate = get_target_pstate_use_cpu_load,
  798. },
  799. };
  800. static struct cpu_defaults knl_params = {
  801. .pid_policy = {
  802. .sample_rate_ms = 10,
  803. .deadband = 0,
  804. .setpoint = 97,
  805. .p_gain_pct = 20,
  806. .d_gain_pct = 0,
  807. .i_gain_pct = 0,
  808. },
  809. .funcs = {
  810. .get_max = core_get_max_pstate,
  811. .get_max_physical = core_get_max_pstate_physical,
  812. .get_min = core_get_min_pstate,
  813. .get_turbo = knl_get_turbo_pstate,
  814. .get_scaling = core_get_scaling,
  815. .get_val = core_get_val,
  816. .get_target_pstate = get_target_pstate_use_performance,
  817. },
  818. };
  819. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  820. {
  821. int max_perf = cpu->pstate.turbo_pstate;
  822. int max_perf_adj;
  823. int min_perf;
  824. if (limits->no_turbo || limits->turbo_disabled)
  825. max_perf = cpu->pstate.max_pstate;
  826. /*
  827. * performance can be limited by user through sysfs, by cpufreq
  828. * policy, or by cpu specific default values determined through
  829. * experimentation.
  830. */
  831. max_perf_adj = fp_toint(max_perf * limits->max_perf);
  832. *max = clamp_t(int, max_perf_adj,
  833. cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
  834. min_perf = fp_toint(max_perf * limits->min_perf);
  835. *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
  836. }
  837. static inline void intel_pstate_record_pstate(struct cpudata *cpu, int pstate)
  838. {
  839. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  840. cpu->pstate.current_pstate = pstate;
  841. }
  842. static void intel_pstate_set_min_pstate(struct cpudata *cpu)
  843. {
  844. int pstate = cpu->pstate.min_pstate;
  845. intel_pstate_record_pstate(cpu, pstate);
  846. /*
  847. * Generally, there is no guarantee that this code will always run on
  848. * the CPU being updated, so force the register update to run on the
  849. * right CPU.
  850. */
  851. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  852. pstate_funcs.get_val(cpu, pstate));
  853. }
  854. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  855. {
  856. cpu->pstate.min_pstate = pstate_funcs.get_min();
  857. cpu->pstate.max_pstate = pstate_funcs.get_max();
  858. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  859. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  860. cpu->pstate.scaling = pstate_funcs.get_scaling();
  861. if (pstate_funcs.get_vid)
  862. pstate_funcs.get_vid(cpu);
  863. intel_pstate_set_min_pstate(cpu);
  864. }
  865. static inline void intel_pstate_calc_busy(struct cpudata *cpu)
  866. {
  867. struct sample *sample = &cpu->sample;
  868. int64_t core_pct;
  869. core_pct = int_tofp(sample->aperf) * int_tofp(100);
  870. core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
  871. sample->core_pct_busy = (int32_t)core_pct;
  872. }
  873. static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
  874. {
  875. u64 aperf, mperf;
  876. unsigned long flags;
  877. u64 tsc;
  878. local_irq_save(flags);
  879. rdmsrl(MSR_IA32_APERF, aperf);
  880. rdmsrl(MSR_IA32_MPERF, mperf);
  881. tsc = rdtsc();
  882. if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
  883. local_irq_restore(flags);
  884. return false;
  885. }
  886. local_irq_restore(flags);
  887. cpu->last_sample_time = cpu->sample.time;
  888. cpu->sample.time = time;
  889. cpu->sample.aperf = aperf;
  890. cpu->sample.mperf = mperf;
  891. cpu->sample.tsc = tsc;
  892. cpu->sample.aperf -= cpu->prev_aperf;
  893. cpu->sample.mperf -= cpu->prev_mperf;
  894. cpu->sample.tsc -= cpu->prev_tsc;
  895. cpu->prev_aperf = aperf;
  896. cpu->prev_mperf = mperf;
  897. cpu->prev_tsc = tsc;
  898. /*
  899. * First time this function is invoked in a given cycle, all of the
  900. * previous sample data fields are equal to zero or stale and they must
  901. * be populated with meaningful numbers for things to work, so assume
  902. * that sample.time will always be reset before setting the utilization
  903. * update hook and make the caller skip the sample then.
  904. */
  905. return !!cpu->last_sample_time;
  906. }
  907. static inline int32_t get_avg_frequency(struct cpudata *cpu)
  908. {
  909. return div64_u64(cpu->pstate.max_pstate_physical * cpu->sample.aperf *
  910. cpu->pstate.scaling, cpu->sample.mperf);
  911. }
  912. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
  913. {
  914. struct sample *sample = &cpu->sample;
  915. u64 cummulative_iowait, delta_iowait_us;
  916. u64 delta_iowait_mperf;
  917. u64 mperf, now;
  918. int32_t cpu_load;
  919. cummulative_iowait = get_cpu_iowait_time_us(cpu->cpu, &now);
  920. /*
  921. * Convert iowait time into number of IO cycles spent at max_freq.
  922. * IO is considered as busy only for the cpu_load algorithm. For
  923. * performance this is not needed since we always try to reach the
  924. * maximum P-State, so we are already boosting the IOs.
  925. */
  926. delta_iowait_us = cummulative_iowait - cpu->prev_cummulative_iowait;
  927. delta_iowait_mperf = div64_u64(delta_iowait_us * cpu->pstate.scaling *
  928. cpu->pstate.max_pstate, MSEC_PER_SEC);
  929. mperf = cpu->sample.mperf + delta_iowait_mperf;
  930. cpu->prev_cummulative_iowait = cummulative_iowait;
  931. /*
  932. * The load can be estimated as the ratio of the mperf counter
  933. * running at a constant frequency during active periods
  934. * (C0) and the time stamp counter running at the same frequency
  935. * also during C-states.
  936. */
  937. cpu_load = div64_u64(int_tofp(100) * mperf, sample->tsc);
  938. cpu->sample.busy_scaled = cpu_load;
  939. return cpu->pstate.current_pstate - pid_calc(&cpu->pid, cpu_load);
  940. }
  941. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
  942. {
  943. int32_t core_busy, max_pstate, current_pstate, sample_ratio;
  944. u64 duration_ns;
  945. intel_pstate_calc_busy(cpu);
  946. /*
  947. * core_busy is the ratio of actual performance to max
  948. * max_pstate is the max non turbo pstate available
  949. * current_pstate was the pstate that was requested during
  950. * the last sample period.
  951. *
  952. * We normalize core_busy, which was our actual percent
  953. * performance to what we requested during the last sample
  954. * period. The result will be a percentage of busy at a
  955. * specified pstate.
  956. */
  957. core_busy = cpu->sample.core_pct_busy;
  958. max_pstate = int_tofp(cpu->pstate.max_pstate_physical);
  959. current_pstate = int_tofp(cpu->pstate.current_pstate);
  960. core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
  961. /*
  962. * Since our utilization update callback will not run unless we are
  963. * in C0, check if the actual elapsed time is significantly greater (3x)
  964. * than our sample interval. If it is, then we were idle for a long
  965. * enough period of time to adjust our busyness.
  966. */
  967. duration_ns = cpu->sample.time - cpu->last_sample_time;
  968. if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
  969. sample_ratio = div_fp(int_tofp(pid_params.sample_rate_ns),
  970. int_tofp(duration_ns));
  971. core_busy = mul_fp(core_busy, sample_ratio);
  972. }
  973. cpu->sample.busy_scaled = core_busy;
  974. return cpu->pstate.current_pstate - pid_calc(&cpu->pid, core_busy);
  975. }
  976. static inline void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
  977. {
  978. int max_perf, min_perf;
  979. update_turbo_state();
  980. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  981. pstate = clamp_t(int, pstate, min_perf, max_perf);
  982. if (pstate == cpu->pstate.current_pstate)
  983. return;
  984. intel_pstate_record_pstate(cpu, pstate);
  985. wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
  986. }
  987. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  988. {
  989. int from, target_pstate;
  990. struct sample *sample;
  991. from = cpu->pstate.current_pstate;
  992. target_pstate = pstate_funcs.get_target_pstate(cpu);
  993. intel_pstate_update_pstate(cpu, target_pstate);
  994. sample = &cpu->sample;
  995. trace_pstate_sample(fp_toint(sample->core_pct_busy),
  996. fp_toint(sample->busy_scaled),
  997. from,
  998. cpu->pstate.current_pstate,
  999. sample->mperf,
  1000. sample->aperf,
  1001. sample->tsc,
  1002. get_avg_frequency(cpu));
  1003. }
  1004. static void intel_pstate_update_util(struct update_util_data *data, u64 time,
  1005. unsigned long util, unsigned long max)
  1006. {
  1007. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1008. u64 delta_ns = time - cpu->sample.time;
  1009. if ((s64)delta_ns >= pid_params.sample_rate_ns) {
  1010. bool sample_taken = intel_pstate_sample(cpu, time);
  1011. if (sample_taken && !hwp_active)
  1012. intel_pstate_adjust_busy_pstate(cpu);
  1013. }
  1014. }
  1015. #define ICPU(model, policy) \
  1016. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  1017. (unsigned long)&policy }
  1018. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  1019. ICPU(0x2a, core_params),
  1020. ICPU(0x2d, core_params),
  1021. ICPU(0x37, silvermont_params),
  1022. ICPU(0x3a, core_params),
  1023. ICPU(0x3c, core_params),
  1024. ICPU(0x3d, core_params),
  1025. ICPU(0x3e, core_params),
  1026. ICPU(0x3f, core_params),
  1027. ICPU(0x45, core_params),
  1028. ICPU(0x46, core_params),
  1029. ICPU(0x47, core_params),
  1030. ICPU(0x4c, airmont_params),
  1031. ICPU(0x4e, core_params),
  1032. ICPU(0x4f, core_params),
  1033. ICPU(0x5e, core_params),
  1034. ICPU(0x56, core_params),
  1035. ICPU(0x57, knl_params),
  1036. {}
  1037. };
  1038. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  1039. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
  1040. ICPU(0x56, core_params),
  1041. {}
  1042. };
  1043. static int intel_pstate_init_cpu(unsigned int cpunum)
  1044. {
  1045. struct cpudata *cpu;
  1046. if (!all_cpu_data[cpunum])
  1047. all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
  1048. GFP_KERNEL);
  1049. if (!all_cpu_data[cpunum])
  1050. return -ENOMEM;
  1051. cpu = all_cpu_data[cpunum];
  1052. cpu->cpu = cpunum;
  1053. if (hwp_active) {
  1054. intel_pstate_hwp_enable(cpu);
  1055. pid_params.sample_rate_ms = 50;
  1056. pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
  1057. }
  1058. intel_pstate_get_cpu_pstates(cpu);
  1059. intel_pstate_busy_pid_reset(cpu);
  1060. cpu->update_util.func = intel_pstate_update_util;
  1061. pr_debug("intel_pstate: controlling: cpu %d\n", cpunum);
  1062. return 0;
  1063. }
  1064. static unsigned int intel_pstate_get(unsigned int cpu_num)
  1065. {
  1066. struct sample *sample;
  1067. struct cpudata *cpu;
  1068. cpu = all_cpu_data[cpu_num];
  1069. if (!cpu)
  1070. return 0;
  1071. sample = &cpu->sample;
  1072. return get_avg_frequency(cpu);
  1073. }
  1074. static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
  1075. {
  1076. struct cpudata *cpu = all_cpu_data[cpu_num];
  1077. /* Prevent intel_pstate_update_util() from using stale data. */
  1078. cpu->sample.time = 0;
  1079. cpufreq_set_update_util_data(cpu_num, &cpu->update_util);
  1080. }
  1081. static void intel_pstate_clear_update_util_hook(unsigned int cpu)
  1082. {
  1083. cpufreq_set_update_util_data(cpu, NULL);
  1084. synchronize_sched();
  1085. }
  1086. static void intel_pstate_set_performance_limits(struct perf_limits *limits)
  1087. {
  1088. limits->no_turbo = 0;
  1089. limits->turbo_disabled = 0;
  1090. limits->max_perf_pct = 100;
  1091. limits->max_perf = int_tofp(1);
  1092. limits->min_perf_pct = 100;
  1093. limits->min_perf = int_tofp(1);
  1094. limits->max_policy_pct = 100;
  1095. limits->max_sysfs_pct = 100;
  1096. limits->min_policy_pct = 0;
  1097. limits->min_sysfs_pct = 0;
  1098. }
  1099. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  1100. {
  1101. if (!policy->cpuinfo.max_freq)
  1102. return -ENODEV;
  1103. intel_pstate_clear_update_util_hook(policy->cpu);
  1104. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  1105. limits = &performance_limits;
  1106. if (policy->max >= policy->cpuinfo.max_freq) {
  1107. pr_debug("intel_pstate: set performance\n");
  1108. intel_pstate_set_performance_limits(limits);
  1109. goto out;
  1110. }
  1111. } else {
  1112. pr_debug("intel_pstate: set powersave\n");
  1113. limits = &powersave_limits;
  1114. }
  1115. limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
  1116. limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
  1117. limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
  1118. policy->cpuinfo.max_freq);
  1119. limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
  1120. /* Normalize user input to [min_policy_pct, max_policy_pct] */
  1121. limits->min_perf_pct = max(limits->min_policy_pct,
  1122. limits->min_sysfs_pct);
  1123. limits->min_perf_pct = min(limits->max_policy_pct,
  1124. limits->min_perf_pct);
  1125. limits->max_perf_pct = min(limits->max_policy_pct,
  1126. limits->max_sysfs_pct);
  1127. limits->max_perf_pct = max(limits->min_policy_pct,
  1128. limits->max_perf_pct);
  1129. limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
  1130. /* Make sure min_perf_pct <= max_perf_pct */
  1131. limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
  1132. limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
  1133. int_tofp(100));
  1134. limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
  1135. int_tofp(100));
  1136. out:
  1137. intel_pstate_set_update_util_hook(policy->cpu);
  1138. if (hwp_active)
  1139. intel_pstate_hwp_set(policy->cpus);
  1140. return 0;
  1141. }
  1142. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  1143. {
  1144. cpufreq_verify_within_cpu_limits(policy);
  1145. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  1146. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  1147. return -EINVAL;
  1148. return 0;
  1149. }
  1150. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  1151. {
  1152. int cpu_num = policy->cpu;
  1153. struct cpudata *cpu = all_cpu_data[cpu_num];
  1154. pr_debug("intel_pstate: CPU %d exiting\n", cpu_num);
  1155. intel_pstate_clear_update_util_hook(cpu_num);
  1156. if (hwp_active)
  1157. return;
  1158. intel_pstate_set_min_pstate(cpu);
  1159. }
  1160. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1161. {
  1162. struct cpudata *cpu;
  1163. int rc;
  1164. rc = intel_pstate_init_cpu(policy->cpu);
  1165. if (rc)
  1166. return rc;
  1167. cpu = all_cpu_data[policy->cpu];
  1168. if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
  1169. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1170. else
  1171. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1172. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1173. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1174. /* cpuinfo and default policy values */
  1175. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1176. policy->cpuinfo.max_freq =
  1177. cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1178. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  1179. cpumask_set_cpu(policy->cpu, policy->cpus);
  1180. return 0;
  1181. }
  1182. static struct cpufreq_driver intel_pstate_driver = {
  1183. .flags = CPUFREQ_CONST_LOOPS,
  1184. .verify = intel_pstate_verify_policy,
  1185. .setpolicy = intel_pstate_set_policy,
  1186. .get = intel_pstate_get,
  1187. .init = intel_pstate_cpu_init,
  1188. .stop_cpu = intel_pstate_stop_cpu,
  1189. .name = "intel_pstate",
  1190. };
  1191. static int __initdata no_load;
  1192. static int __initdata no_hwp;
  1193. static int __initdata hwp_only;
  1194. static unsigned int force_load;
  1195. static int intel_pstate_msrs_not_valid(void)
  1196. {
  1197. if (!pstate_funcs.get_max() ||
  1198. !pstate_funcs.get_min() ||
  1199. !pstate_funcs.get_turbo())
  1200. return -ENODEV;
  1201. return 0;
  1202. }
  1203. static void copy_pid_params(struct pstate_adjust_policy *policy)
  1204. {
  1205. pid_params.sample_rate_ms = policy->sample_rate_ms;
  1206. pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
  1207. pid_params.p_gain_pct = policy->p_gain_pct;
  1208. pid_params.i_gain_pct = policy->i_gain_pct;
  1209. pid_params.d_gain_pct = policy->d_gain_pct;
  1210. pid_params.deadband = policy->deadband;
  1211. pid_params.setpoint = policy->setpoint;
  1212. }
  1213. static void copy_cpu_funcs(struct pstate_funcs *funcs)
  1214. {
  1215. pstate_funcs.get_max = funcs->get_max;
  1216. pstate_funcs.get_max_physical = funcs->get_max_physical;
  1217. pstate_funcs.get_min = funcs->get_min;
  1218. pstate_funcs.get_turbo = funcs->get_turbo;
  1219. pstate_funcs.get_scaling = funcs->get_scaling;
  1220. pstate_funcs.get_val = funcs->get_val;
  1221. pstate_funcs.get_vid = funcs->get_vid;
  1222. pstate_funcs.get_target_pstate = funcs->get_target_pstate;
  1223. }
  1224. #if IS_ENABLED(CONFIG_ACPI)
  1225. #include <acpi/processor.h>
  1226. static bool intel_pstate_no_acpi_pss(void)
  1227. {
  1228. int i;
  1229. for_each_possible_cpu(i) {
  1230. acpi_status status;
  1231. union acpi_object *pss;
  1232. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1233. struct acpi_processor *pr = per_cpu(processors, i);
  1234. if (!pr)
  1235. continue;
  1236. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  1237. if (ACPI_FAILURE(status))
  1238. continue;
  1239. pss = buffer.pointer;
  1240. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  1241. kfree(pss);
  1242. return false;
  1243. }
  1244. kfree(pss);
  1245. }
  1246. return true;
  1247. }
  1248. static bool intel_pstate_has_acpi_ppc(void)
  1249. {
  1250. int i;
  1251. for_each_possible_cpu(i) {
  1252. struct acpi_processor *pr = per_cpu(processors, i);
  1253. if (!pr)
  1254. continue;
  1255. if (acpi_has_method(pr->handle, "_PPC"))
  1256. return true;
  1257. }
  1258. return false;
  1259. }
  1260. enum {
  1261. PSS,
  1262. PPC,
  1263. };
  1264. struct hw_vendor_info {
  1265. u16 valid;
  1266. char oem_id[ACPI_OEM_ID_SIZE];
  1267. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  1268. int oem_pwr_table;
  1269. };
  1270. /* Hardware vendor-specific info that has its own power management modes */
  1271. static struct hw_vendor_info vendor_info[] = {
  1272. {1, "HP ", "ProLiant", PSS},
  1273. {1, "ORACLE", "X4-2 ", PPC},
  1274. {1, "ORACLE", "X4-2L ", PPC},
  1275. {1, "ORACLE", "X4-2B ", PPC},
  1276. {1, "ORACLE", "X3-2 ", PPC},
  1277. {1, "ORACLE", "X3-2L ", PPC},
  1278. {1, "ORACLE", "X3-2B ", PPC},
  1279. {1, "ORACLE", "X4470M2 ", PPC},
  1280. {1, "ORACLE", "X4270M3 ", PPC},
  1281. {1, "ORACLE", "X4270M2 ", PPC},
  1282. {1, "ORACLE", "X4170M2 ", PPC},
  1283. {1, "ORACLE", "X4170 M3", PPC},
  1284. {1, "ORACLE", "X4275 M3", PPC},
  1285. {1, "ORACLE", "X6-2 ", PPC},
  1286. {1, "ORACLE", "Sudbury ", PPC},
  1287. {0, "", ""},
  1288. };
  1289. static bool intel_pstate_platform_pwr_mgmt_exists(void)
  1290. {
  1291. struct acpi_table_header hdr;
  1292. struct hw_vendor_info *v_info;
  1293. const struct x86_cpu_id *id;
  1294. u64 misc_pwr;
  1295. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  1296. if (id) {
  1297. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  1298. if ( misc_pwr & (1 << 8))
  1299. return true;
  1300. }
  1301. if (acpi_disabled ||
  1302. ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
  1303. return false;
  1304. for (v_info = vendor_info; v_info->valid; v_info++) {
  1305. if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
  1306. !strncmp(hdr.oem_table_id, v_info->oem_table_id,
  1307. ACPI_OEM_TABLE_ID_SIZE))
  1308. switch (v_info->oem_pwr_table) {
  1309. case PSS:
  1310. return intel_pstate_no_acpi_pss();
  1311. case PPC:
  1312. return intel_pstate_has_acpi_ppc() &&
  1313. (!force_load);
  1314. }
  1315. }
  1316. return false;
  1317. }
  1318. #else /* CONFIG_ACPI not enabled */
  1319. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  1320. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  1321. #endif /* CONFIG_ACPI */
  1322. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  1323. { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
  1324. {}
  1325. };
  1326. static int __init intel_pstate_init(void)
  1327. {
  1328. int cpu, rc = 0;
  1329. const struct x86_cpu_id *id;
  1330. struct cpu_defaults *cpu_def;
  1331. if (no_load)
  1332. return -ENODEV;
  1333. if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
  1334. copy_cpu_funcs(&core_params.funcs);
  1335. hwp_active++;
  1336. goto hwp_cpu_matched;
  1337. }
  1338. id = x86_match_cpu(intel_pstate_cpu_ids);
  1339. if (!id)
  1340. return -ENODEV;
  1341. cpu_def = (struct cpu_defaults *)id->driver_data;
  1342. copy_pid_params(&cpu_def->pid_policy);
  1343. copy_cpu_funcs(&cpu_def->funcs);
  1344. if (intel_pstate_msrs_not_valid())
  1345. return -ENODEV;
  1346. hwp_cpu_matched:
  1347. /*
  1348. * The Intel pstate driver will be ignored if the platform
  1349. * firmware has its own power management modes.
  1350. */
  1351. if (intel_pstate_platform_pwr_mgmt_exists())
  1352. return -ENODEV;
  1353. pr_info("Intel P-state driver initializing.\n");
  1354. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  1355. if (!all_cpu_data)
  1356. return -ENOMEM;
  1357. if (!hwp_active && hwp_only)
  1358. goto out;
  1359. rc = cpufreq_register_driver(&intel_pstate_driver);
  1360. if (rc)
  1361. goto out;
  1362. intel_pstate_debug_expose_params();
  1363. intel_pstate_sysfs_expose_params();
  1364. if (hwp_active)
  1365. pr_info("intel_pstate: HWP enabled\n");
  1366. return rc;
  1367. out:
  1368. get_online_cpus();
  1369. for_each_online_cpu(cpu) {
  1370. if (all_cpu_data[cpu]) {
  1371. intel_pstate_clear_update_util_hook(cpu);
  1372. kfree(all_cpu_data[cpu]);
  1373. }
  1374. }
  1375. put_online_cpus();
  1376. vfree(all_cpu_data);
  1377. return -ENODEV;
  1378. }
  1379. device_initcall(intel_pstate_init);
  1380. static int __init intel_pstate_setup(char *str)
  1381. {
  1382. if (!str)
  1383. return -EINVAL;
  1384. if (!strcmp(str, "disable"))
  1385. no_load = 1;
  1386. if (!strcmp(str, "no_hwp")) {
  1387. pr_info("intel_pstate: HWP disabled\n");
  1388. no_hwp = 1;
  1389. }
  1390. if (!strcmp(str, "force"))
  1391. force_load = 1;
  1392. if (!strcmp(str, "hwp_only"))
  1393. hwp_only = 1;
  1394. return 0;
  1395. }
  1396. early_param("intel_pstate", intel_pstate_setup);
  1397. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  1398. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  1399. MODULE_LICENSE("GPL");