intel_pstate.c 46 KB

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