intel_pstate.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  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. #include <asm/intel-family.h>
  36. #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
  37. #define ATOM_RATIOS 0x66a
  38. #define ATOM_VIDS 0x66b
  39. #define ATOM_TURBO_RATIOS 0x66c
  40. #define ATOM_TURBO_VIDS 0x66d
  41. #ifdef CONFIG_ACPI
  42. #include <acpi/processor.h>
  43. #include <acpi/cppc_acpi.h>
  44. #endif
  45. #define FRAC_BITS 8
  46. #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  47. #define fp_toint(X) ((X) >> FRAC_BITS)
  48. #define EXT_BITS 6
  49. #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
  50. #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
  51. #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
  52. static inline int32_t mul_fp(int32_t x, int32_t y)
  53. {
  54. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  55. }
  56. static inline int32_t div_fp(s64 x, s64 y)
  57. {
  58. return div64_s64((int64_t)x << FRAC_BITS, y);
  59. }
  60. static inline int ceiling_fp(int32_t x)
  61. {
  62. int mask, ret;
  63. ret = fp_toint(x);
  64. mask = (1 << FRAC_BITS) - 1;
  65. if (x & mask)
  66. ret += 1;
  67. return ret;
  68. }
  69. static inline u64 mul_ext_fp(u64 x, u64 y)
  70. {
  71. return (x * y) >> EXT_FRAC_BITS;
  72. }
  73. static inline u64 div_ext_fp(u64 x, u64 y)
  74. {
  75. return div64_u64(x << EXT_FRAC_BITS, y);
  76. }
  77. /**
  78. * struct sample - Store performance sample
  79. * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
  80. * performance during last sample period
  81. * @busy_scaled: Scaled busy value which is used to calculate next
  82. * P state. This can be different than core_avg_perf
  83. * to account for cpu idle period
  84. * @aperf: Difference of actual performance frequency clock count
  85. * read from APERF MSR between last and current sample
  86. * @mperf: Difference of maximum performance frequency clock count
  87. * read from MPERF MSR between last and current sample
  88. * @tsc: Difference of time stamp counter between last and
  89. * current sample
  90. * @time: Current time from scheduler
  91. *
  92. * This structure is used in the cpudata structure to store performance sample
  93. * data for choosing next P State.
  94. */
  95. struct sample {
  96. int32_t core_avg_perf;
  97. int32_t busy_scaled;
  98. u64 aperf;
  99. u64 mperf;
  100. u64 tsc;
  101. u64 time;
  102. };
  103. /**
  104. * struct pstate_data - Store P state data
  105. * @current_pstate: Current requested P state
  106. * @min_pstate: Min P state possible for this platform
  107. * @max_pstate: Max P state possible for this platform
  108. * @max_pstate_physical:This is physical Max P state for a processor
  109. * This can be higher than the max_pstate which can
  110. * be limited by platform thermal design power limits
  111. * @scaling: Scaling factor to convert frequency to cpufreq
  112. * frequency units
  113. * @turbo_pstate: Max Turbo P state possible for this platform
  114. * @max_freq: @max_pstate frequency in cpufreq units
  115. * @turbo_freq: @turbo_pstate frequency in cpufreq units
  116. *
  117. * Stores the per cpu model P state limits and current P state.
  118. */
  119. struct pstate_data {
  120. int current_pstate;
  121. int min_pstate;
  122. int max_pstate;
  123. int max_pstate_physical;
  124. int scaling;
  125. int turbo_pstate;
  126. unsigned int max_freq;
  127. unsigned int turbo_freq;
  128. };
  129. /**
  130. * struct vid_data - Stores voltage information data
  131. * @min: VID data for this platform corresponding to
  132. * the lowest P state
  133. * @max: VID data corresponding to the highest P State.
  134. * @turbo: VID data for turbo P state
  135. * @ratio: Ratio of (vid max - vid min) /
  136. * (max P state - Min P State)
  137. *
  138. * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
  139. * This data is used in Atom platforms, where in addition to target P state,
  140. * the voltage data needs to be specified to select next P State.
  141. */
  142. struct vid_data {
  143. int min;
  144. int max;
  145. int turbo;
  146. int32_t ratio;
  147. };
  148. /**
  149. * struct _pid - Stores PID data
  150. * @setpoint: Target set point for busyness or performance
  151. * @integral: Storage for accumulated error values
  152. * @p_gain: PID proportional gain
  153. * @i_gain: PID integral gain
  154. * @d_gain: PID derivative gain
  155. * @deadband: PID deadband
  156. * @last_err: Last error storage for integral part of PID calculation
  157. *
  158. * Stores PID coefficients and last error for PID controller.
  159. */
  160. struct _pid {
  161. int setpoint;
  162. int32_t integral;
  163. int32_t p_gain;
  164. int32_t i_gain;
  165. int32_t d_gain;
  166. int deadband;
  167. int32_t last_err;
  168. };
  169. /**
  170. * struct perf_limits - Store user and policy limits
  171. * @no_turbo: User requested turbo state from intel_pstate sysfs
  172. * @turbo_disabled: Platform turbo status either from msr
  173. * MSR_IA32_MISC_ENABLE or when maximum available pstate
  174. * matches the maximum turbo pstate
  175. * @max_perf_pct: Effective maximum performance limit in percentage, this
  176. * is minimum of either limits enforced by cpufreq policy
  177. * or limits from user set limits via intel_pstate sysfs
  178. * @min_perf_pct: Effective minimum performance limit in percentage, this
  179. * is maximum of either limits enforced by cpufreq policy
  180. * or limits from user set limits via intel_pstate sysfs
  181. * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
  182. * This value is used to limit max pstate
  183. * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
  184. * This value is used to limit min pstate
  185. * @max_policy_pct: The maximum performance in percentage enforced by
  186. * cpufreq setpolicy interface
  187. * @max_sysfs_pct: The maximum performance in percentage enforced by
  188. * intel pstate sysfs interface, unused when per cpu
  189. * controls are enforced
  190. * @min_policy_pct: The minimum performance in percentage enforced by
  191. * cpufreq setpolicy interface
  192. * @min_sysfs_pct: The minimum performance in percentage enforced by
  193. * intel pstate sysfs interface, unused when per cpu
  194. * controls are enforced
  195. *
  196. * Storage for user and policy defined limits.
  197. */
  198. struct perf_limits {
  199. int no_turbo;
  200. int turbo_disabled;
  201. int max_perf_pct;
  202. int min_perf_pct;
  203. int32_t max_perf;
  204. int32_t min_perf;
  205. int max_policy_pct;
  206. int max_sysfs_pct;
  207. int min_policy_pct;
  208. int min_sysfs_pct;
  209. };
  210. /**
  211. * struct cpudata - Per CPU instance data storage
  212. * @cpu: CPU number for this instance data
  213. * @policy: CPUFreq policy value
  214. * @update_util: CPUFreq utility callback information
  215. * @update_util_set: CPUFreq utility callback is set
  216. * @iowait_boost: iowait-related boost fraction
  217. * @last_update: Time of the last update.
  218. * @pstate: Stores P state limits for this CPU
  219. * @vid: Stores VID limits for this CPU
  220. * @pid: Stores PID parameters for this CPU
  221. * @last_sample_time: Last Sample time
  222. * @prev_aperf: Last APERF value read from APERF MSR
  223. * @prev_mperf: Last MPERF value read from MPERF MSR
  224. * @prev_tsc: Last timestamp counter (TSC) value
  225. * @prev_cummulative_iowait: IO Wait time difference from last and
  226. * current sample
  227. * @sample: Storage for storing last Sample data
  228. * @perf_limits: Pointer to perf_limit unique to this CPU
  229. * Not all field in the structure are applicable
  230. * when per cpu controls are enforced
  231. * @acpi_perf_data: Stores ACPI perf information read from _PSS
  232. * @valid_pss_table: Set to true for valid ACPI _PSS entries found
  233. * @epp_powersave: Last saved HWP energy performance preference
  234. * (EPP) or energy performance bias (EPB),
  235. * when policy switched to performance
  236. * @epp_policy: Last saved policy used to set EPP/EPB
  237. * @epp_default: Power on default HWP energy performance
  238. * preference/bias
  239. * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
  240. * operation
  241. *
  242. * This structure stores per CPU instance data for all CPUs.
  243. */
  244. struct cpudata {
  245. int cpu;
  246. unsigned int policy;
  247. struct update_util_data update_util;
  248. bool update_util_set;
  249. struct pstate_data pstate;
  250. struct vid_data vid;
  251. struct _pid pid;
  252. u64 last_update;
  253. u64 last_sample_time;
  254. u64 prev_aperf;
  255. u64 prev_mperf;
  256. u64 prev_tsc;
  257. u64 prev_cummulative_iowait;
  258. struct sample sample;
  259. struct perf_limits *perf_limits;
  260. #ifdef CONFIG_ACPI
  261. struct acpi_processor_performance acpi_perf_data;
  262. bool valid_pss_table;
  263. #endif
  264. unsigned int iowait_boost;
  265. s16 epp_powersave;
  266. s16 epp_policy;
  267. s16 epp_default;
  268. s16 epp_saved;
  269. };
  270. static struct cpudata **all_cpu_data;
  271. /**
  272. * struct pstate_adjust_policy - Stores static PID configuration data
  273. * @sample_rate_ms: PID calculation sample rate in ms
  274. * @sample_rate_ns: Sample rate calculation in ns
  275. * @deadband: PID deadband
  276. * @setpoint: PID Setpoint
  277. * @p_gain_pct: PID proportional gain
  278. * @i_gain_pct: PID integral gain
  279. * @d_gain_pct: PID derivative gain
  280. *
  281. * Stores per CPU model static PID configuration data.
  282. */
  283. struct pstate_adjust_policy {
  284. int sample_rate_ms;
  285. s64 sample_rate_ns;
  286. int deadband;
  287. int setpoint;
  288. int p_gain_pct;
  289. int d_gain_pct;
  290. int i_gain_pct;
  291. };
  292. /**
  293. * struct pstate_funcs - Per CPU model specific callbacks
  294. * @get_max: Callback to get maximum non turbo effective P state
  295. * @get_max_physical: Callback to get maximum non turbo physical P state
  296. * @get_min: Callback to get minimum P state
  297. * @get_turbo: Callback to get turbo P state
  298. * @get_scaling: Callback to get frequency scaling factor
  299. * @get_val: Callback to convert P state to actual MSR write value
  300. * @get_vid: Callback to get VID data for Atom platforms
  301. * @get_target_pstate: Callback to a function to calculate next P state to use
  302. *
  303. * Core and Atom CPU models have different way to get P State limits. This
  304. * structure is used to store those callbacks.
  305. */
  306. struct pstate_funcs {
  307. int (*get_max)(void);
  308. int (*get_max_physical)(void);
  309. int (*get_min)(void);
  310. int (*get_turbo)(void);
  311. int (*get_scaling)(void);
  312. u64 (*get_val)(struct cpudata*, int pstate);
  313. void (*get_vid)(struct cpudata *);
  314. int32_t (*get_target_pstate)(struct cpudata *);
  315. };
  316. /**
  317. * struct cpu_defaults- Per CPU model default config data
  318. * @pid_policy: PID config data
  319. * @funcs: Callback function data
  320. */
  321. struct cpu_defaults {
  322. struct pstate_adjust_policy pid_policy;
  323. struct pstate_funcs funcs;
  324. };
  325. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
  326. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
  327. static struct pstate_adjust_policy pid_params __read_mostly;
  328. static struct pstate_funcs pstate_funcs __read_mostly;
  329. static int hwp_active __read_mostly;
  330. static bool per_cpu_limits __read_mostly;
  331. static bool driver_registered __read_mostly;
  332. #ifdef CONFIG_ACPI
  333. static bool acpi_ppc;
  334. #endif
  335. static struct perf_limits performance_limits = {
  336. .no_turbo = 0,
  337. .turbo_disabled = 0,
  338. .max_perf_pct = 100,
  339. .max_perf = int_ext_tofp(1),
  340. .min_perf_pct = 100,
  341. .min_perf = int_ext_tofp(1),
  342. .max_policy_pct = 100,
  343. .max_sysfs_pct = 100,
  344. .min_policy_pct = 0,
  345. .min_sysfs_pct = 0,
  346. };
  347. static struct perf_limits powersave_limits = {
  348. .no_turbo = 0,
  349. .turbo_disabled = 0,
  350. .max_perf_pct = 100,
  351. .max_perf = int_ext_tofp(1),
  352. .min_perf_pct = 0,
  353. .min_perf = 0,
  354. .max_policy_pct = 100,
  355. .max_sysfs_pct = 100,
  356. .min_policy_pct = 0,
  357. .min_sysfs_pct = 0,
  358. };
  359. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  360. static struct perf_limits *limits = &performance_limits;
  361. #else
  362. static struct perf_limits *limits = &powersave_limits;
  363. #endif
  364. static DEFINE_MUTEX(intel_pstate_driver_lock);
  365. static DEFINE_MUTEX(intel_pstate_limits_lock);
  366. #ifdef CONFIG_ACPI
  367. static bool intel_pstate_get_ppc_enable_status(void)
  368. {
  369. if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
  370. acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
  371. return true;
  372. return acpi_ppc;
  373. }
  374. #ifdef CONFIG_ACPI_CPPC_LIB
  375. /* The work item is needed to avoid CPU hotplug locking issues */
  376. static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
  377. {
  378. sched_set_itmt_support();
  379. }
  380. static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
  381. static void intel_pstate_set_itmt_prio(int cpu)
  382. {
  383. struct cppc_perf_caps cppc_perf;
  384. static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
  385. int ret;
  386. ret = cppc_get_perf_caps(cpu, &cppc_perf);
  387. if (ret)
  388. return;
  389. /*
  390. * The priorities can be set regardless of whether or not
  391. * sched_set_itmt_support(true) has been called and it is valid to
  392. * update them at any time after it has been called.
  393. */
  394. sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
  395. if (max_highest_perf <= min_highest_perf) {
  396. if (cppc_perf.highest_perf > max_highest_perf)
  397. max_highest_perf = cppc_perf.highest_perf;
  398. if (cppc_perf.highest_perf < min_highest_perf)
  399. min_highest_perf = cppc_perf.highest_perf;
  400. if (max_highest_perf > min_highest_perf) {
  401. /*
  402. * This code can be run during CPU online under the
  403. * CPU hotplug locks, so sched_set_itmt_support()
  404. * cannot be called from here. Queue up a work item
  405. * to invoke it.
  406. */
  407. schedule_work(&sched_itmt_work);
  408. }
  409. }
  410. }
  411. #else
  412. static void intel_pstate_set_itmt_prio(int cpu)
  413. {
  414. }
  415. #endif
  416. static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  417. {
  418. struct cpudata *cpu;
  419. int ret;
  420. int i;
  421. if (hwp_active) {
  422. intel_pstate_set_itmt_prio(policy->cpu);
  423. return;
  424. }
  425. if (!intel_pstate_get_ppc_enable_status())
  426. return;
  427. cpu = all_cpu_data[policy->cpu];
  428. ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
  429. policy->cpu);
  430. if (ret)
  431. return;
  432. /*
  433. * Check if the control value in _PSS is for PERF_CTL MSR, which should
  434. * guarantee that the states returned by it map to the states in our
  435. * list directly.
  436. */
  437. if (cpu->acpi_perf_data.control_register.space_id !=
  438. ACPI_ADR_SPACE_FIXED_HARDWARE)
  439. goto err;
  440. /*
  441. * If there is only one entry _PSS, simply ignore _PSS and continue as
  442. * usual without taking _PSS into account
  443. */
  444. if (cpu->acpi_perf_data.state_count < 2)
  445. goto err;
  446. pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
  447. for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
  448. pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
  449. (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
  450. (u32) cpu->acpi_perf_data.states[i].core_frequency,
  451. (u32) cpu->acpi_perf_data.states[i].power,
  452. (u32) cpu->acpi_perf_data.states[i].control);
  453. }
  454. /*
  455. * The _PSS table doesn't contain whole turbo frequency range.
  456. * This just contains +1 MHZ above the max non turbo frequency,
  457. * with control value corresponding to max turbo ratio. But
  458. * when cpufreq set policy is called, it will call with this
  459. * max frequency, which will cause a reduced performance as
  460. * this driver uses real max turbo frequency as the max
  461. * frequency. So correct this frequency in _PSS table to
  462. * correct max turbo frequency based on the turbo state.
  463. * Also need to convert to MHz as _PSS freq is in MHz.
  464. */
  465. if (!limits->turbo_disabled)
  466. cpu->acpi_perf_data.states[0].core_frequency =
  467. policy->cpuinfo.max_freq / 1000;
  468. cpu->valid_pss_table = true;
  469. pr_debug("_PPC limits will be enforced\n");
  470. return;
  471. err:
  472. cpu->valid_pss_table = false;
  473. acpi_processor_unregister_performance(policy->cpu);
  474. }
  475. static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  476. {
  477. struct cpudata *cpu;
  478. cpu = all_cpu_data[policy->cpu];
  479. if (!cpu->valid_pss_table)
  480. return;
  481. acpi_processor_unregister_performance(policy->cpu);
  482. }
  483. #else
  484. static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  485. {
  486. }
  487. static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  488. {
  489. }
  490. #endif
  491. static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
  492. int deadband, int integral) {
  493. pid->setpoint = int_tofp(setpoint);
  494. pid->deadband = int_tofp(deadband);
  495. pid->integral = int_tofp(integral);
  496. pid->last_err = int_tofp(setpoint) - int_tofp(busy);
  497. }
  498. static inline void pid_p_gain_set(struct _pid *pid, int percent)
  499. {
  500. pid->p_gain = div_fp(percent, 100);
  501. }
  502. static inline void pid_i_gain_set(struct _pid *pid, int percent)
  503. {
  504. pid->i_gain = div_fp(percent, 100);
  505. }
  506. static inline void pid_d_gain_set(struct _pid *pid, int percent)
  507. {
  508. pid->d_gain = div_fp(percent, 100);
  509. }
  510. static signed int pid_calc(struct _pid *pid, int32_t busy)
  511. {
  512. signed int result;
  513. int32_t pterm, dterm, fp_error;
  514. int32_t integral_limit;
  515. fp_error = pid->setpoint - busy;
  516. if (abs(fp_error) <= pid->deadband)
  517. return 0;
  518. pterm = mul_fp(pid->p_gain, fp_error);
  519. pid->integral += fp_error;
  520. /*
  521. * We limit the integral here so that it will never
  522. * get higher than 30. This prevents it from becoming
  523. * too large an input over long periods of time and allows
  524. * it to get factored out sooner.
  525. *
  526. * The value of 30 was chosen through experimentation.
  527. */
  528. integral_limit = int_tofp(30);
  529. if (pid->integral > integral_limit)
  530. pid->integral = integral_limit;
  531. if (pid->integral < -integral_limit)
  532. pid->integral = -integral_limit;
  533. dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
  534. pid->last_err = fp_error;
  535. result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
  536. result = result + (1 << (FRAC_BITS-1));
  537. return (signed int)fp_toint(result);
  538. }
  539. static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
  540. {
  541. pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
  542. pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
  543. pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
  544. pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
  545. }
  546. static inline void intel_pstate_reset_all_pid(void)
  547. {
  548. unsigned int cpu;
  549. for_each_online_cpu(cpu) {
  550. if (all_cpu_data[cpu])
  551. intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
  552. }
  553. }
  554. static inline void update_turbo_state(void)
  555. {
  556. u64 misc_en;
  557. struct cpudata *cpu;
  558. cpu = all_cpu_data[0];
  559. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  560. limits->turbo_disabled =
  561. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  562. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  563. }
  564. static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
  565. {
  566. u64 epb;
  567. int ret;
  568. if (!static_cpu_has(X86_FEATURE_EPB))
  569. return -ENXIO;
  570. ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  571. if (ret)
  572. return (s16)ret;
  573. return (s16)(epb & 0x0f);
  574. }
  575. static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
  576. {
  577. s16 epp;
  578. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  579. /*
  580. * When hwp_req_data is 0, means that caller didn't read
  581. * MSR_HWP_REQUEST, so need to read and get EPP.
  582. */
  583. if (!hwp_req_data) {
  584. epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
  585. &hwp_req_data);
  586. if (epp)
  587. return epp;
  588. }
  589. epp = (hwp_req_data >> 24) & 0xff;
  590. } else {
  591. /* When there is no EPP present, HWP uses EPB settings */
  592. epp = intel_pstate_get_epb(cpu_data);
  593. }
  594. return epp;
  595. }
  596. static int intel_pstate_set_epb(int cpu, s16 pref)
  597. {
  598. u64 epb;
  599. int ret;
  600. if (!static_cpu_has(X86_FEATURE_EPB))
  601. return -ENXIO;
  602. ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  603. if (ret)
  604. return ret;
  605. epb = (epb & ~0x0f) | pref;
  606. wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
  607. return 0;
  608. }
  609. /*
  610. * EPP/EPB display strings corresponding to EPP index in the
  611. * energy_perf_strings[]
  612. * index String
  613. *-------------------------------------
  614. * 0 default
  615. * 1 performance
  616. * 2 balance_performance
  617. * 3 balance_power
  618. * 4 power
  619. */
  620. static const char * const energy_perf_strings[] = {
  621. "default",
  622. "performance",
  623. "balance_performance",
  624. "balance_power",
  625. "power",
  626. NULL
  627. };
  628. static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
  629. {
  630. s16 epp;
  631. int index = -EINVAL;
  632. epp = intel_pstate_get_epp(cpu_data, 0);
  633. if (epp < 0)
  634. return epp;
  635. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  636. /*
  637. * Range:
  638. * 0x00-0x3F : Performance
  639. * 0x40-0x7F : Balance performance
  640. * 0x80-0xBF : Balance power
  641. * 0xC0-0xFF : Power
  642. * The EPP is a 8 bit value, but our ranges restrict the
  643. * value which can be set. Here only using top two bits
  644. * effectively.
  645. */
  646. index = (epp >> 6) + 1;
  647. } else if (static_cpu_has(X86_FEATURE_EPB)) {
  648. /*
  649. * Range:
  650. * 0x00-0x03 : Performance
  651. * 0x04-0x07 : Balance performance
  652. * 0x08-0x0B : Balance power
  653. * 0x0C-0x0F : Power
  654. * The EPB is a 4 bit value, but our ranges restrict the
  655. * value which can be set. Here only using top two bits
  656. * effectively.
  657. */
  658. index = (epp >> 2) + 1;
  659. }
  660. return index;
  661. }
  662. static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
  663. int pref_index)
  664. {
  665. int epp = -EINVAL;
  666. int ret;
  667. if (!pref_index)
  668. epp = cpu_data->epp_default;
  669. mutex_lock(&intel_pstate_limits_lock);
  670. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  671. u64 value;
  672. ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
  673. if (ret)
  674. goto return_pref;
  675. value &= ~GENMASK_ULL(31, 24);
  676. /*
  677. * If epp is not default, convert from index into
  678. * energy_perf_strings to epp value, by shifting 6
  679. * bits left to use only top two bits in epp.
  680. * The resultant epp need to shifted by 24 bits to
  681. * epp position in MSR_HWP_REQUEST.
  682. */
  683. if (epp == -EINVAL)
  684. epp = (pref_index - 1) << 6;
  685. value |= (u64)epp << 24;
  686. ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
  687. } else {
  688. if (epp == -EINVAL)
  689. epp = (pref_index - 1) << 2;
  690. ret = intel_pstate_set_epb(cpu_data->cpu, epp);
  691. }
  692. return_pref:
  693. mutex_unlock(&intel_pstate_limits_lock);
  694. return ret;
  695. }
  696. static ssize_t show_energy_performance_available_preferences(
  697. struct cpufreq_policy *policy, char *buf)
  698. {
  699. int i = 0;
  700. int ret = 0;
  701. while (energy_perf_strings[i] != NULL)
  702. ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
  703. ret += sprintf(&buf[ret], "\n");
  704. return ret;
  705. }
  706. cpufreq_freq_attr_ro(energy_performance_available_preferences);
  707. static ssize_t store_energy_performance_preference(
  708. struct cpufreq_policy *policy, const char *buf, size_t count)
  709. {
  710. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  711. char str_preference[21];
  712. int ret, i = 0;
  713. ret = sscanf(buf, "%20s", str_preference);
  714. if (ret != 1)
  715. return -EINVAL;
  716. while (energy_perf_strings[i] != NULL) {
  717. if (!strcmp(str_preference, energy_perf_strings[i])) {
  718. intel_pstate_set_energy_pref_index(cpu_data, i);
  719. return count;
  720. }
  721. ++i;
  722. }
  723. return -EINVAL;
  724. }
  725. static ssize_t show_energy_performance_preference(
  726. struct cpufreq_policy *policy, char *buf)
  727. {
  728. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  729. int preference;
  730. preference = intel_pstate_get_energy_pref_index(cpu_data);
  731. if (preference < 0)
  732. return preference;
  733. return sprintf(buf, "%s\n", energy_perf_strings[preference]);
  734. }
  735. cpufreq_freq_attr_rw(energy_performance_preference);
  736. static struct freq_attr *hwp_cpufreq_attrs[] = {
  737. &energy_performance_preference,
  738. &energy_performance_available_preferences,
  739. NULL,
  740. };
  741. static void intel_pstate_hwp_set(struct cpufreq_policy *policy)
  742. {
  743. int min, hw_min, max, hw_max, cpu, range, adj_range;
  744. struct perf_limits *perf_limits = limits;
  745. u64 value, cap;
  746. for_each_cpu(cpu, policy->cpus) {
  747. int max_perf_pct, min_perf_pct;
  748. struct cpudata *cpu_data = all_cpu_data[cpu];
  749. s16 epp;
  750. if (per_cpu_limits)
  751. perf_limits = all_cpu_data[cpu]->perf_limits;
  752. rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
  753. hw_min = HWP_LOWEST_PERF(cap);
  754. hw_max = HWP_HIGHEST_PERF(cap);
  755. range = hw_max - hw_min;
  756. max_perf_pct = perf_limits->max_perf_pct;
  757. min_perf_pct = perf_limits->min_perf_pct;
  758. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  759. adj_range = min_perf_pct * range / 100;
  760. min = hw_min + adj_range;
  761. value &= ~HWP_MIN_PERF(~0L);
  762. value |= HWP_MIN_PERF(min);
  763. adj_range = max_perf_pct * range / 100;
  764. max = hw_min + adj_range;
  765. if (limits->no_turbo) {
  766. hw_max = HWP_GUARANTEED_PERF(cap);
  767. if (hw_max < max)
  768. max = hw_max;
  769. }
  770. value &= ~HWP_MAX_PERF(~0L);
  771. value |= HWP_MAX_PERF(max);
  772. if (cpu_data->epp_policy == cpu_data->policy)
  773. goto skip_epp;
  774. cpu_data->epp_policy = cpu_data->policy;
  775. if (cpu_data->epp_saved >= 0) {
  776. epp = cpu_data->epp_saved;
  777. cpu_data->epp_saved = -EINVAL;
  778. goto update_epp;
  779. }
  780. if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
  781. epp = intel_pstate_get_epp(cpu_data, value);
  782. cpu_data->epp_powersave = epp;
  783. /* If EPP read was failed, then don't try to write */
  784. if (epp < 0)
  785. goto skip_epp;
  786. epp = 0;
  787. } else {
  788. /* skip setting EPP, when saved value is invalid */
  789. if (cpu_data->epp_powersave < 0)
  790. goto skip_epp;
  791. /*
  792. * No need to restore EPP when it is not zero. This
  793. * means:
  794. * - Policy is not changed
  795. * - user has manually changed
  796. * - Error reading EPB
  797. */
  798. epp = intel_pstate_get_epp(cpu_data, value);
  799. if (epp)
  800. goto skip_epp;
  801. epp = cpu_data->epp_powersave;
  802. }
  803. update_epp:
  804. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  805. value &= ~GENMASK_ULL(31, 24);
  806. value |= (u64)epp << 24;
  807. } else {
  808. intel_pstate_set_epb(cpu, epp);
  809. }
  810. skip_epp:
  811. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  812. }
  813. }
  814. static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
  815. {
  816. if (hwp_active)
  817. intel_pstate_hwp_set(policy);
  818. return 0;
  819. }
  820. static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
  821. {
  822. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  823. if (!hwp_active)
  824. return 0;
  825. cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
  826. return 0;
  827. }
  828. static int intel_pstate_resume(struct cpufreq_policy *policy)
  829. {
  830. int ret;
  831. if (!hwp_active)
  832. return 0;
  833. mutex_lock(&intel_pstate_limits_lock);
  834. all_cpu_data[policy->cpu]->epp_policy = 0;
  835. ret = intel_pstate_hwp_set_policy(policy);
  836. mutex_unlock(&intel_pstate_limits_lock);
  837. return ret;
  838. }
  839. static void intel_pstate_update_policies(void)
  840. {
  841. int cpu;
  842. for_each_possible_cpu(cpu)
  843. cpufreq_update_policy(cpu);
  844. }
  845. /************************** debugfs begin ************************/
  846. static int pid_param_set(void *data, u64 val)
  847. {
  848. *(u32 *)data = val;
  849. intel_pstate_reset_all_pid();
  850. return 0;
  851. }
  852. static int pid_param_get(void *data, u64 *val)
  853. {
  854. *val = *(u32 *)data;
  855. return 0;
  856. }
  857. DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
  858. static struct dentry *debugfs_parent;
  859. struct pid_param {
  860. char *name;
  861. void *value;
  862. struct dentry *dentry;
  863. };
  864. static struct pid_param pid_files[] = {
  865. {"sample_rate_ms", &pid_params.sample_rate_ms, },
  866. {"d_gain_pct", &pid_params.d_gain_pct, },
  867. {"i_gain_pct", &pid_params.i_gain_pct, },
  868. {"deadband", &pid_params.deadband, },
  869. {"setpoint", &pid_params.setpoint, },
  870. {"p_gain_pct", &pid_params.p_gain_pct, },
  871. {NULL, NULL, }
  872. };
  873. static void intel_pstate_debug_expose_params(void)
  874. {
  875. int i;
  876. debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
  877. if (IS_ERR_OR_NULL(debugfs_parent))
  878. return;
  879. for (i = 0; pid_files[i].name; i++) {
  880. struct dentry *dentry;
  881. dentry = debugfs_create_file(pid_files[i].name, 0660,
  882. debugfs_parent, pid_files[i].value,
  883. &fops_pid_param);
  884. if (!IS_ERR(dentry))
  885. pid_files[i].dentry = dentry;
  886. }
  887. }
  888. static void intel_pstate_debug_hide_params(void)
  889. {
  890. int i;
  891. if (IS_ERR_OR_NULL(debugfs_parent))
  892. return;
  893. for (i = 0; pid_files[i].name; i++) {
  894. debugfs_remove(pid_files[i].dentry);
  895. pid_files[i].dentry = NULL;
  896. }
  897. debugfs_remove(debugfs_parent);
  898. debugfs_parent = NULL;
  899. }
  900. /************************** debugfs end ************************/
  901. /************************** sysfs begin ************************/
  902. #define show_one(file_name, object) \
  903. static ssize_t show_##file_name \
  904. (struct kobject *kobj, struct attribute *attr, char *buf) \
  905. { \
  906. return sprintf(buf, "%u\n", limits->object); \
  907. }
  908. static ssize_t intel_pstate_show_status(char *buf);
  909. static int intel_pstate_update_status(const char *buf, size_t size);
  910. static ssize_t show_status(struct kobject *kobj,
  911. struct attribute *attr, char *buf)
  912. {
  913. ssize_t ret;
  914. mutex_lock(&intel_pstate_driver_lock);
  915. ret = intel_pstate_show_status(buf);
  916. mutex_unlock(&intel_pstate_driver_lock);
  917. return ret;
  918. }
  919. static ssize_t store_status(struct kobject *a, struct attribute *b,
  920. const char *buf, size_t count)
  921. {
  922. char *p = memchr(buf, '\n', count);
  923. int ret;
  924. mutex_lock(&intel_pstate_driver_lock);
  925. ret = intel_pstate_update_status(buf, p ? p - buf : count);
  926. mutex_unlock(&intel_pstate_driver_lock);
  927. return ret < 0 ? ret : count;
  928. }
  929. static ssize_t show_turbo_pct(struct kobject *kobj,
  930. struct attribute *attr, char *buf)
  931. {
  932. struct cpudata *cpu;
  933. int total, no_turbo, turbo_pct;
  934. uint32_t turbo_fp;
  935. mutex_lock(&intel_pstate_driver_lock);
  936. if (!driver_registered) {
  937. mutex_unlock(&intel_pstate_driver_lock);
  938. return -EAGAIN;
  939. }
  940. cpu = all_cpu_data[0];
  941. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  942. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  943. turbo_fp = div_fp(no_turbo, total);
  944. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  945. mutex_unlock(&intel_pstate_driver_lock);
  946. return sprintf(buf, "%u\n", turbo_pct);
  947. }
  948. static ssize_t show_num_pstates(struct kobject *kobj,
  949. struct attribute *attr, char *buf)
  950. {
  951. struct cpudata *cpu;
  952. int total;
  953. mutex_lock(&intel_pstate_driver_lock);
  954. if (!driver_registered) {
  955. mutex_unlock(&intel_pstate_driver_lock);
  956. return -EAGAIN;
  957. }
  958. cpu = all_cpu_data[0];
  959. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  960. mutex_unlock(&intel_pstate_driver_lock);
  961. return sprintf(buf, "%u\n", total);
  962. }
  963. static ssize_t show_no_turbo(struct kobject *kobj,
  964. struct attribute *attr, char *buf)
  965. {
  966. ssize_t ret;
  967. mutex_lock(&intel_pstate_driver_lock);
  968. if (!driver_registered) {
  969. mutex_unlock(&intel_pstate_driver_lock);
  970. return -EAGAIN;
  971. }
  972. update_turbo_state();
  973. if (limits->turbo_disabled)
  974. ret = sprintf(buf, "%u\n", limits->turbo_disabled);
  975. else
  976. ret = sprintf(buf, "%u\n", limits->no_turbo);
  977. mutex_unlock(&intel_pstate_driver_lock);
  978. return ret;
  979. }
  980. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  981. const char *buf, size_t count)
  982. {
  983. unsigned int input;
  984. int ret;
  985. ret = sscanf(buf, "%u", &input);
  986. if (ret != 1)
  987. return -EINVAL;
  988. mutex_lock(&intel_pstate_driver_lock);
  989. if (!driver_registered) {
  990. mutex_unlock(&intel_pstate_driver_lock);
  991. return -EAGAIN;
  992. }
  993. mutex_lock(&intel_pstate_limits_lock);
  994. update_turbo_state();
  995. if (limits->turbo_disabled) {
  996. pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
  997. mutex_unlock(&intel_pstate_limits_lock);
  998. mutex_unlock(&intel_pstate_driver_lock);
  999. return -EPERM;
  1000. }
  1001. limits->no_turbo = clamp_t(int, input, 0, 1);
  1002. mutex_unlock(&intel_pstate_limits_lock);
  1003. intel_pstate_update_policies();
  1004. mutex_unlock(&intel_pstate_driver_lock);
  1005. return count;
  1006. }
  1007. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  1008. const char *buf, size_t count)
  1009. {
  1010. unsigned int input;
  1011. int ret;
  1012. ret = sscanf(buf, "%u", &input);
  1013. if (ret != 1)
  1014. return -EINVAL;
  1015. mutex_lock(&intel_pstate_driver_lock);
  1016. if (!driver_registered) {
  1017. mutex_unlock(&intel_pstate_driver_lock);
  1018. return -EAGAIN;
  1019. }
  1020. mutex_lock(&intel_pstate_limits_lock);
  1021. limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
  1022. limits->max_perf_pct = min(limits->max_policy_pct,
  1023. limits->max_sysfs_pct);
  1024. limits->max_perf_pct = max(limits->min_policy_pct,
  1025. limits->max_perf_pct);
  1026. limits->max_perf_pct = max(limits->min_perf_pct,
  1027. limits->max_perf_pct);
  1028. limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
  1029. mutex_unlock(&intel_pstate_limits_lock);
  1030. intel_pstate_update_policies();
  1031. mutex_unlock(&intel_pstate_driver_lock);
  1032. return count;
  1033. }
  1034. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  1035. const char *buf, size_t count)
  1036. {
  1037. unsigned int input;
  1038. int ret;
  1039. ret = sscanf(buf, "%u", &input);
  1040. if (ret != 1)
  1041. return -EINVAL;
  1042. mutex_lock(&intel_pstate_driver_lock);
  1043. if (!driver_registered) {
  1044. mutex_unlock(&intel_pstate_driver_lock);
  1045. return -EAGAIN;
  1046. }
  1047. mutex_lock(&intel_pstate_limits_lock);
  1048. limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
  1049. limits->min_perf_pct = max(limits->min_policy_pct,
  1050. limits->min_sysfs_pct);
  1051. limits->min_perf_pct = min(limits->max_policy_pct,
  1052. limits->min_perf_pct);
  1053. limits->min_perf_pct = min(limits->max_perf_pct,
  1054. limits->min_perf_pct);
  1055. limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
  1056. mutex_unlock(&intel_pstate_limits_lock);
  1057. intel_pstate_update_policies();
  1058. mutex_unlock(&intel_pstate_driver_lock);
  1059. return count;
  1060. }
  1061. show_one(max_perf_pct, max_perf_pct);
  1062. show_one(min_perf_pct, min_perf_pct);
  1063. define_one_global_rw(status);
  1064. define_one_global_rw(no_turbo);
  1065. define_one_global_rw(max_perf_pct);
  1066. define_one_global_rw(min_perf_pct);
  1067. define_one_global_ro(turbo_pct);
  1068. define_one_global_ro(num_pstates);
  1069. static struct attribute *intel_pstate_attributes[] = {
  1070. &status.attr,
  1071. &no_turbo.attr,
  1072. &turbo_pct.attr,
  1073. &num_pstates.attr,
  1074. NULL
  1075. };
  1076. static struct attribute_group intel_pstate_attr_group = {
  1077. .attrs = intel_pstate_attributes,
  1078. };
  1079. static void __init intel_pstate_sysfs_expose_params(void)
  1080. {
  1081. struct kobject *intel_pstate_kobject;
  1082. int rc;
  1083. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  1084. &cpu_subsys.dev_root->kobj);
  1085. if (WARN_ON(!intel_pstate_kobject))
  1086. return;
  1087. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  1088. if (WARN_ON(rc))
  1089. return;
  1090. /*
  1091. * If per cpu limits are enforced there are no global limits, so
  1092. * return without creating max/min_perf_pct attributes
  1093. */
  1094. if (per_cpu_limits)
  1095. return;
  1096. rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
  1097. WARN_ON(rc);
  1098. rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
  1099. WARN_ON(rc);
  1100. }
  1101. /************************** sysfs end ************************/
  1102. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  1103. {
  1104. /* First disable HWP notification interrupt as we don't process them */
  1105. if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
  1106. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  1107. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  1108. cpudata->epp_policy = 0;
  1109. if (cpudata->epp_default == -EINVAL)
  1110. cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
  1111. }
  1112. static int atom_get_min_pstate(void)
  1113. {
  1114. u64 value;
  1115. rdmsrl(ATOM_RATIOS, value);
  1116. return (value >> 8) & 0x7F;
  1117. }
  1118. static int atom_get_max_pstate(void)
  1119. {
  1120. u64 value;
  1121. rdmsrl(ATOM_RATIOS, value);
  1122. return (value >> 16) & 0x7F;
  1123. }
  1124. static int atom_get_turbo_pstate(void)
  1125. {
  1126. u64 value;
  1127. rdmsrl(ATOM_TURBO_RATIOS, value);
  1128. return value & 0x7F;
  1129. }
  1130. static u64 atom_get_val(struct cpudata *cpudata, int pstate)
  1131. {
  1132. u64 val;
  1133. int32_t vid_fp;
  1134. u32 vid;
  1135. val = (u64)pstate << 8;
  1136. if (limits->no_turbo && !limits->turbo_disabled)
  1137. val |= (u64)1 << 32;
  1138. vid_fp = cpudata->vid.min + mul_fp(
  1139. int_tofp(pstate - cpudata->pstate.min_pstate),
  1140. cpudata->vid.ratio);
  1141. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  1142. vid = ceiling_fp(vid_fp);
  1143. if (pstate > cpudata->pstate.max_pstate)
  1144. vid = cpudata->vid.turbo;
  1145. return val | vid;
  1146. }
  1147. static int silvermont_get_scaling(void)
  1148. {
  1149. u64 value;
  1150. int i;
  1151. /* Defined in Table 35-6 from SDM (Sept 2015) */
  1152. static int silvermont_freq_table[] = {
  1153. 83300, 100000, 133300, 116700, 80000};
  1154. rdmsrl(MSR_FSB_FREQ, value);
  1155. i = value & 0x7;
  1156. WARN_ON(i > 4);
  1157. return silvermont_freq_table[i];
  1158. }
  1159. static int airmont_get_scaling(void)
  1160. {
  1161. u64 value;
  1162. int i;
  1163. /* Defined in Table 35-10 from SDM (Sept 2015) */
  1164. static int airmont_freq_table[] = {
  1165. 83300, 100000, 133300, 116700, 80000,
  1166. 93300, 90000, 88900, 87500};
  1167. rdmsrl(MSR_FSB_FREQ, value);
  1168. i = value & 0xF;
  1169. WARN_ON(i > 8);
  1170. return airmont_freq_table[i];
  1171. }
  1172. static void atom_get_vid(struct cpudata *cpudata)
  1173. {
  1174. u64 value;
  1175. rdmsrl(ATOM_VIDS, value);
  1176. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  1177. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  1178. cpudata->vid.ratio = div_fp(
  1179. cpudata->vid.max - cpudata->vid.min,
  1180. int_tofp(cpudata->pstate.max_pstate -
  1181. cpudata->pstate.min_pstate));
  1182. rdmsrl(ATOM_TURBO_VIDS, value);
  1183. cpudata->vid.turbo = value & 0x7f;
  1184. }
  1185. static int core_get_min_pstate(void)
  1186. {
  1187. u64 value;
  1188. rdmsrl(MSR_PLATFORM_INFO, value);
  1189. return (value >> 40) & 0xFF;
  1190. }
  1191. static int core_get_max_pstate_physical(void)
  1192. {
  1193. u64 value;
  1194. rdmsrl(MSR_PLATFORM_INFO, value);
  1195. return (value >> 8) & 0xFF;
  1196. }
  1197. static int core_get_max_pstate(void)
  1198. {
  1199. u64 tar;
  1200. u64 plat_info;
  1201. int max_pstate;
  1202. int err;
  1203. rdmsrl(MSR_PLATFORM_INFO, plat_info);
  1204. max_pstate = (plat_info >> 8) & 0xFF;
  1205. err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
  1206. if (!err) {
  1207. /* Do some sanity checking for safety */
  1208. if (plat_info & 0x600000000) {
  1209. u64 tdp_ctrl;
  1210. u64 tdp_ratio;
  1211. int tdp_msr;
  1212. err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  1213. if (err)
  1214. goto skip_tar;
  1215. tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3);
  1216. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  1217. if (err)
  1218. goto skip_tar;
  1219. /* For level 1 and 2, bits[23:16] contain the ratio */
  1220. if (tdp_ctrl)
  1221. tdp_ratio >>= 16;
  1222. tdp_ratio &= 0xff; /* ratios are only 8 bits long */
  1223. if (tdp_ratio - 1 == tar) {
  1224. max_pstate = tar;
  1225. pr_debug("max_pstate=TAC %x\n", max_pstate);
  1226. } else {
  1227. goto skip_tar;
  1228. }
  1229. }
  1230. }
  1231. skip_tar:
  1232. return max_pstate;
  1233. }
  1234. static int core_get_turbo_pstate(void)
  1235. {
  1236. u64 value;
  1237. int nont, ret;
  1238. rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
  1239. nont = core_get_max_pstate();
  1240. ret = (value) & 255;
  1241. if (ret <= nont)
  1242. ret = nont;
  1243. return ret;
  1244. }
  1245. static inline int core_get_scaling(void)
  1246. {
  1247. return 100000;
  1248. }
  1249. static u64 core_get_val(struct cpudata *cpudata, int pstate)
  1250. {
  1251. u64 val;
  1252. val = (u64)pstate << 8;
  1253. if (limits->no_turbo && !limits->turbo_disabled)
  1254. val |= (u64)1 << 32;
  1255. return val;
  1256. }
  1257. static int knl_get_turbo_pstate(void)
  1258. {
  1259. u64 value;
  1260. int nont, ret;
  1261. rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
  1262. nont = core_get_max_pstate();
  1263. ret = (((value) >> 8) & 0xFF);
  1264. if (ret <= nont)
  1265. ret = nont;
  1266. return ret;
  1267. }
  1268. static struct cpu_defaults core_params = {
  1269. .pid_policy = {
  1270. .sample_rate_ms = 10,
  1271. .deadband = 0,
  1272. .setpoint = 97,
  1273. .p_gain_pct = 20,
  1274. .d_gain_pct = 0,
  1275. .i_gain_pct = 0,
  1276. },
  1277. .funcs = {
  1278. .get_max = core_get_max_pstate,
  1279. .get_max_physical = core_get_max_pstate_physical,
  1280. .get_min = core_get_min_pstate,
  1281. .get_turbo = core_get_turbo_pstate,
  1282. .get_scaling = core_get_scaling,
  1283. .get_val = core_get_val,
  1284. .get_target_pstate = get_target_pstate_use_performance,
  1285. },
  1286. };
  1287. static const struct cpu_defaults silvermont_params = {
  1288. .pid_policy = {
  1289. .sample_rate_ms = 10,
  1290. .deadband = 0,
  1291. .setpoint = 60,
  1292. .p_gain_pct = 14,
  1293. .d_gain_pct = 0,
  1294. .i_gain_pct = 4,
  1295. },
  1296. .funcs = {
  1297. .get_max = atom_get_max_pstate,
  1298. .get_max_physical = atom_get_max_pstate,
  1299. .get_min = atom_get_min_pstate,
  1300. .get_turbo = atom_get_turbo_pstate,
  1301. .get_val = atom_get_val,
  1302. .get_scaling = silvermont_get_scaling,
  1303. .get_vid = atom_get_vid,
  1304. .get_target_pstate = get_target_pstate_use_cpu_load,
  1305. },
  1306. };
  1307. static const struct cpu_defaults airmont_params = {
  1308. .pid_policy = {
  1309. .sample_rate_ms = 10,
  1310. .deadband = 0,
  1311. .setpoint = 60,
  1312. .p_gain_pct = 14,
  1313. .d_gain_pct = 0,
  1314. .i_gain_pct = 4,
  1315. },
  1316. .funcs = {
  1317. .get_max = atom_get_max_pstate,
  1318. .get_max_physical = atom_get_max_pstate,
  1319. .get_min = atom_get_min_pstate,
  1320. .get_turbo = atom_get_turbo_pstate,
  1321. .get_val = atom_get_val,
  1322. .get_scaling = airmont_get_scaling,
  1323. .get_vid = atom_get_vid,
  1324. .get_target_pstate = get_target_pstate_use_cpu_load,
  1325. },
  1326. };
  1327. static const struct cpu_defaults knl_params = {
  1328. .pid_policy = {
  1329. .sample_rate_ms = 10,
  1330. .deadband = 0,
  1331. .setpoint = 97,
  1332. .p_gain_pct = 20,
  1333. .d_gain_pct = 0,
  1334. .i_gain_pct = 0,
  1335. },
  1336. .funcs = {
  1337. .get_max = core_get_max_pstate,
  1338. .get_max_physical = core_get_max_pstate_physical,
  1339. .get_min = core_get_min_pstate,
  1340. .get_turbo = knl_get_turbo_pstate,
  1341. .get_scaling = core_get_scaling,
  1342. .get_val = core_get_val,
  1343. .get_target_pstate = get_target_pstate_use_performance,
  1344. },
  1345. };
  1346. static const struct cpu_defaults bxt_params = {
  1347. .pid_policy = {
  1348. .sample_rate_ms = 10,
  1349. .deadband = 0,
  1350. .setpoint = 60,
  1351. .p_gain_pct = 14,
  1352. .d_gain_pct = 0,
  1353. .i_gain_pct = 4,
  1354. },
  1355. .funcs = {
  1356. .get_max = core_get_max_pstate,
  1357. .get_max_physical = core_get_max_pstate_physical,
  1358. .get_min = core_get_min_pstate,
  1359. .get_turbo = core_get_turbo_pstate,
  1360. .get_scaling = core_get_scaling,
  1361. .get_val = core_get_val,
  1362. .get_target_pstate = get_target_pstate_use_cpu_load,
  1363. },
  1364. };
  1365. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  1366. {
  1367. int max_perf = cpu->pstate.turbo_pstate;
  1368. int max_perf_adj;
  1369. int min_perf;
  1370. struct perf_limits *perf_limits = limits;
  1371. if (limits->no_turbo || limits->turbo_disabled)
  1372. max_perf = cpu->pstate.max_pstate;
  1373. if (per_cpu_limits)
  1374. perf_limits = cpu->perf_limits;
  1375. /*
  1376. * performance can be limited by user through sysfs, by cpufreq
  1377. * policy, or by cpu specific default values determined through
  1378. * experimentation.
  1379. */
  1380. max_perf_adj = fp_ext_toint(max_perf * perf_limits->max_perf);
  1381. *max = clamp_t(int, max_perf_adj,
  1382. cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
  1383. min_perf = fp_ext_toint(max_perf * perf_limits->min_perf);
  1384. *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
  1385. }
  1386. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
  1387. {
  1388. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  1389. cpu->pstate.current_pstate = pstate;
  1390. /*
  1391. * Generally, there is no guarantee that this code will always run on
  1392. * the CPU being updated, so force the register update to run on the
  1393. * right CPU.
  1394. */
  1395. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  1396. pstate_funcs.get_val(cpu, pstate));
  1397. }
  1398. static void intel_pstate_set_min_pstate(struct cpudata *cpu)
  1399. {
  1400. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
  1401. }
  1402. static void intel_pstate_max_within_limits(struct cpudata *cpu)
  1403. {
  1404. int min_pstate, max_pstate;
  1405. update_turbo_state();
  1406. intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
  1407. intel_pstate_set_pstate(cpu, max_pstate);
  1408. }
  1409. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  1410. {
  1411. cpu->pstate.min_pstate = pstate_funcs.get_min();
  1412. cpu->pstate.max_pstate = pstate_funcs.get_max();
  1413. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  1414. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  1415. cpu->pstate.scaling = pstate_funcs.get_scaling();
  1416. cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
  1417. cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1418. if (pstate_funcs.get_vid)
  1419. pstate_funcs.get_vid(cpu);
  1420. intel_pstate_set_min_pstate(cpu);
  1421. }
  1422. static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
  1423. {
  1424. struct sample *sample = &cpu->sample;
  1425. sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
  1426. }
  1427. static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
  1428. {
  1429. u64 aperf, mperf;
  1430. unsigned long flags;
  1431. u64 tsc;
  1432. local_irq_save(flags);
  1433. rdmsrl(MSR_IA32_APERF, aperf);
  1434. rdmsrl(MSR_IA32_MPERF, mperf);
  1435. tsc = rdtsc();
  1436. if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
  1437. local_irq_restore(flags);
  1438. return false;
  1439. }
  1440. local_irq_restore(flags);
  1441. cpu->last_sample_time = cpu->sample.time;
  1442. cpu->sample.time = time;
  1443. cpu->sample.aperf = aperf;
  1444. cpu->sample.mperf = mperf;
  1445. cpu->sample.tsc = tsc;
  1446. cpu->sample.aperf -= cpu->prev_aperf;
  1447. cpu->sample.mperf -= cpu->prev_mperf;
  1448. cpu->sample.tsc -= cpu->prev_tsc;
  1449. cpu->prev_aperf = aperf;
  1450. cpu->prev_mperf = mperf;
  1451. cpu->prev_tsc = tsc;
  1452. /*
  1453. * First time this function is invoked in a given cycle, all of the
  1454. * previous sample data fields are equal to zero or stale and they must
  1455. * be populated with meaningful numbers for things to work, so assume
  1456. * that sample.time will always be reset before setting the utilization
  1457. * update hook and make the caller skip the sample then.
  1458. */
  1459. return !!cpu->last_sample_time;
  1460. }
  1461. static inline int32_t get_avg_frequency(struct cpudata *cpu)
  1462. {
  1463. return mul_ext_fp(cpu->sample.core_avg_perf,
  1464. cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
  1465. }
  1466. static inline int32_t get_avg_pstate(struct cpudata *cpu)
  1467. {
  1468. return mul_ext_fp(cpu->pstate.max_pstate_physical,
  1469. cpu->sample.core_avg_perf);
  1470. }
  1471. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
  1472. {
  1473. struct sample *sample = &cpu->sample;
  1474. int32_t busy_frac, boost;
  1475. int target, avg_pstate;
  1476. busy_frac = div_fp(sample->mperf, sample->tsc);
  1477. boost = cpu->iowait_boost;
  1478. cpu->iowait_boost >>= 1;
  1479. if (busy_frac < boost)
  1480. busy_frac = boost;
  1481. sample->busy_scaled = busy_frac * 100;
  1482. target = limits->no_turbo || limits->turbo_disabled ?
  1483. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1484. target += target >> 2;
  1485. target = mul_fp(target, busy_frac);
  1486. if (target < cpu->pstate.min_pstate)
  1487. target = cpu->pstate.min_pstate;
  1488. /*
  1489. * If the average P-state during the previous cycle was higher than the
  1490. * current target, add 50% of the difference to the target to reduce
  1491. * possible performance oscillations and offset possible performance
  1492. * loss related to moving the workload from one CPU to another within
  1493. * a package/module.
  1494. */
  1495. avg_pstate = get_avg_pstate(cpu);
  1496. if (avg_pstate > target)
  1497. target += (avg_pstate - target) >> 1;
  1498. return target;
  1499. }
  1500. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
  1501. {
  1502. int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
  1503. u64 duration_ns;
  1504. /*
  1505. * perf_scaled is the ratio of the average P-state during the last
  1506. * sampling period to the P-state requested last time (in percent).
  1507. *
  1508. * That measures the system's response to the previous P-state
  1509. * selection.
  1510. */
  1511. max_pstate = cpu->pstate.max_pstate_physical;
  1512. current_pstate = cpu->pstate.current_pstate;
  1513. perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
  1514. div_fp(100 * max_pstate, current_pstate));
  1515. /*
  1516. * Since our utilization update callback will not run unless we are
  1517. * in C0, check if the actual elapsed time is significantly greater (3x)
  1518. * than our sample interval. If it is, then we were idle for a long
  1519. * enough period of time to adjust our performance metric.
  1520. */
  1521. duration_ns = cpu->sample.time - cpu->last_sample_time;
  1522. if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
  1523. sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
  1524. perf_scaled = mul_fp(perf_scaled, sample_ratio);
  1525. } else {
  1526. sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
  1527. if (sample_ratio < int_tofp(1))
  1528. perf_scaled = 0;
  1529. }
  1530. cpu->sample.busy_scaled = perf_scaled;
  1531. return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
  1532. }
  1533. static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
  1534. {
  1535. int max_perf, min_perf;
  1536. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  1537. pstate = clamp_t(int, pstate, min_perf, max_perf);
  1538. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  1539. return pstate;
  1540. }
  1541. static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
  1542. {
  1543. pstate = intel_pstate_prepare_request(cpu, pstate);
  1544. if (pstate == cpu->pstate.current_pstate)
  1545. return;
  1546. cpu->pstate.current_pstate = pstate;
  1547. wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
  1548. }
  1549. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  1550. {
  1551. int from, target_pstate;
  1552. struct sample *sample;
  1553. from = cpu->pstate.current_pstate;
  1554. target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
  1555. cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
  1556. update_turbo_state();
  1557. intel_pstate_update_pstate(cpu, target_pstate);
  1558. sample = &cpu->sample;
  1559. trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
  1560. fp_toint(sample->busy_scaled),
  1561. from,
  1562. cpu->pstate.current_pstate,
  1563. sample->mperf,
  1564. sample->aperf,
  1565. sample->tsc,
  1566. get_avg_frequency(cpu),
  1567. fp_toint(cpu->iowait_boost * 100));
  1568. }
  1569. static void intel_pstate_update_util(struct update_util_data *data, u64 time,
  1570. unsigned int flags)
  1571. {
  1572. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1573. u64 delta_ns;
  1574. if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
  1575. if (flags & SCHED_CPUFREQ_IOWAIT) {
  1576. cpu->iowait_boost = int_tofp(1);
  1577. } else if (cpu->iowait_boost) {
  1578. /* Clear iowait_boost if the CPU may have been idle. */
  1579. delta_ns = time - cpu->last_update;
  1580. if (delta_ns > TICK_NSEC)
  1581. cpu->iowait_boost = 0;
  1582. }
  1583. cpu->last_update = time;
  1584. }
  1585. delta_ns = time - cpu->sample.time;
  1586. if ((s64)delta_ns >= pid_params.sample_rate_ns) {
  1587. bool sample_taken = intel_pstate_sample(cpu, time);
  1588. if (sample_taken) {
  1589. intel_pstate_calc_avg_perf(cpu);
  1590. if (!hwp_active)
  1591. intel_pstate_adjust_busy_pstate(cpu);
  1592. }
  1593. }
  1594. }
  1595. #define ICPU(model, policy) \
  1596. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  1597. (unsigned long)&policy }
  1598. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  1599. ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
  1600. ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
  1601. ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
  1602. ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
  1603. ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
  1604. ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
  1605. ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
  1606. ICPU(INTEL_FAM6_HASWELL_X, core_params),
  1607. ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
  1608. ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
  1609. ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
  1610. ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
  1611. ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
  1612. ICPU(INTEL_FAM6_BROADWELL_X, core_params),
  1613. ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
  1614. ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
  1615. ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
  1616. ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_params),
  1617. ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
  1618. {}
  1619. };
  1620. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  1621. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
  1622. ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
  1623. ICPU(INTEL_FAM6_BROADWELL_X, core_params),
  1624. ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
  1625. {}
  1626. };
  1627. static int intel_pstate_init_cpu(unsigned int cpunum)
  1628. {
  1629. struct cpudata *cpu;
  1630. cpu = all_cpu_data[cpunum];
  1631. if (!cpu) {
  1632. unsigned int size = sizeof(struct cpudata);
  1633. if (per_cpu_limits)
  1634. size += sizeof(struct perf_limits);
  1635. cpu = kzalloc(size, GFP_KERNEL);
  1636. if (!cpu)
  1637. return -ENOMEM;
  1638. all_cpu_data[cpunum] = cpu;
  1639. if (per_cpu_limits)
  1640. cpu->perf_limits = (struct perf_limits *)(cpu + 1);
  1641. cpu->epp_default = -EINVAL;
  1642. cpu->epp_powersave = -EINVAL;
  1643. cpu->epp_saved = -EINVAL;
  1644. }
  1645. cpu = all_cpu_data[cpunum];
  1646. cpu->cpu = cpunum;
  1647. if (hwp_active) {
  1648. intel_pstate_hwp_enable(cpu);
  1649. pid_params.sample_rate_ms = 50;
  1650. pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
  1651. }
  1652. intel_pstate_get_cpu_pstates(cpu);
  1653. intel_pstate_busy_pid_reset(cpu);
  1654. pr_debug("controlling: cpu %d\n", cpunum);
  1655. return 0;
  1656. }
  1657. static unsigned int intel_pstate_get(unsigned int cpu_num)
  1658. {
  1659. struct cpudata *cpu = all_cpu_data[cpu_num];
  1660. return cpu ? get_avg_frequency(cpu) : 0;
  1661. }
  1662. static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
  1663. {
  1664. struct cpudata *cpu = all_cpu_data[cpu_num];
  1665. if (cpu->update_util_set)
  1666. return;
  1667. /* Prevent intel_pstate_update_util() from using stale data. */
  1668. cpu->sample.time = 0;
  1669. cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
  1670. intel_pstate_update_util);
  1671. cpu->update_util_set = true;
  1672. }
  1673. static void intel_pstate_clear_update_util_hook(unsigned int cpu)
  1674. {
  1675. struct cpudata *cpu_data = all_cpu_data[cpu];
  1676. if (!cpu_data->update_util_set)
  1677. return;
  1678. cpufreq_remove_update_util_hook(cpu);
  1679. cpu_data->update_util_set = false;
  1680. synchronize_sched();
  1681. }
  1682. static void intel_pstate_set_performance_limits(struct perf_limits *limits)
  1683. {
  1684. limits->no_turbo = 0;
  1685. limits->turbo_disabled = 0;
  1686. limits->max_perf_pct = 100;
  1687. limits->max_perf = int_ext_tofp(1);
  1688. limits->min_perf_pct = 100;
  1689. limits->min_perf = int_ext_tofp(1);
  1690. limits->max_policy_pct = 100;
  1691. limits->max_sysfs_pct = 100;
  1692. limits->min_policy_pct = 0;
  1693. limits->min_sysfs_pct = 0;
  1694. }
  1695. static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
  1696. struct perf_limits *limits)
  1697. {
  1698. limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
  1699. policy->cpuinfo.max_freq);
  1700. limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100);
  1701. if (policy->max == policy->min) {
  1702. limits->min_policy_pct = limits->max_policy_pct;
  1703. } else {
  1704. limits->min_policy_pct = DIV_ROUND_UP(policy->min * 100,
  1705. policy->cpuinfo.max_freq);
  1706. limits->min_policy_pct = clamp_t(int, limits->min_policy_pct,
  1707. 0, 100);
  1708. }
  1709. /* Normalize user input to [min_policy_pct, max_policy_pct] */
  1710. limits->min_perf_pct = max(limits->min_policy_pct,
  1711. limits->min_sysfs_pct);
  1712. limits->min_perf_pct = min(limits->max_policy_pct,
  1713. limits->min_perf_pct);
  1714. limits->max_perf_pct = min(limits->max_policy_pct,
  1715. limits->max_sysfs_pct);
  1716. limits->max_perf_pct = max(limits->min_policy_pct,
  1717. limits->max_perf_pct);
  1718. /* Make sure min_perf_pct <= max_perf_pct */
  1719. limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
  1720. limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
  1721. limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
  1722. limits->max_perf = round_up(limits->max_perf, EXT_FRAC_BITS);
  1723. limits->min_perf = round_up(limits->min_perf, EXT_FRAC_BITS);
  1724. pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
  1725. limits->max_perf_pct, limits->min_perf_pct);
  1726. }
  1727. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  1728. {
  1729. struct cpudata *cpu;
  1730. struct perf_limits *perf_limits = NULL;
  1731. if (!policy->cpuinfo.max_freq)
  1732. return -ENODEV;
  1733. pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
  1734. policy->cpuinfo.max_freq, policy->max);
  1735. cpu = all_cpu_data[policy->cpu];
  1736. cpu->policy = policy->policy;
  1737. if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
  1738. policy->max < policy->cpuinfo.max_freq &&
  1739. policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
  1740. pr_debug("policy->max > max non turbo frequency\n");
  1741. policy->max = policy->cpuinfo.max_freq;
  1742. }
  1743. if (per_cpu_limits)
  1744. perf_limits = cpu->perf_limits;
  1745. mutex_lock(&intel_pstate_limits_lock);
  1746. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  1747. if (!perf_limits) {
  1748. limits = &performance_limits;
  1749. perf_limits = limits;
  1750. }
  1751. if (policy->max >= policy->cpuinfo.max_freq &&
  1752. !limits->no_turbo) {
  1753. pr_debug("set performance\n");
  1754. intel_pstate_set_performance_limits(perf_limits);
  1755. goto out;
  1756. }
  1757. } else {
  1758. pr_debug("set powersave\n");
  1759. if (!perf_limits) {
  1760. limits = &powersave_limits;
  1761. perf_limits = limits;
  1762. }
  1763. }
  1764. intel_pstate_update_perf_limits(policy, perf_limits);
  1765. out:
  1766. if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
  1767. /*
  1768. * NOHZ_FULL CPUs need this as the governor callback may not
  1769. * be invoked on them.
  1770. */
  1771. intel_pstate_clear_update_util_hook(policy->cpu);
  1772. intel_pstate_max_within_limits(cpu);
  1773. }
  1774. intel_pstate_set_update_util_hook(policy->cpu);
  1775. intel_pstate_hwp_set_policy(policy);
  1776. mutex_unlock(&intel_pstate_limits_lock);
  1777. return 0;
  1778. }
  1779. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  1780. {
  1781. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1782. struct perf_limits *perf_limits;
  1783. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  1784. perf_limits = &performance_limits;
  1785. else
  1786. perf_limits = &powersave_limits;
  1787. update_turbo_state();
  1788. policy->cpuinfo.max_freq = perf_limits->turbo_disabled ||
  1789. perf_limits->no_turbo ?
  1790. cpu->pstate.max_freq :
  1791. cpu->pstate.turbo_freq;
  1792. cpufreq_verify_within_cpu_limits(policy);
  1793. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  1794. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  1795. return -EINVAL;
  1796. /* When per-CPU limits are used, sysfs limits are not used */
  1797. if (!per_cpu_limits) {
  1798. unsigned int max_freq, min_freq;
  1799. max_freq = policy->cpuinfo.max_freq *
  1800. limits->max_sysfs_pct / 100;
  1801. min_freq = policy->cpuinfo.max_freq *
  1802. limits->min_sysfs_pct / 100;
  1803. cpufreq_verify_within_limits(policy, min_freq, max_freq);
  1804. }
  1805. return 0;
  1806. }
  1807. static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
  1808. {
  1809. intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
  1810. }
  1811. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  1812. {
  1813. pr_debug("CPU %d exiting\n", policy->cpu);
  1814. intel_pstate_clear_update_util_hook(policy->cpu);
  1815. if (hwp_active)
  1816. intel_pstate_hwp_save_state(policy);
  1817. else
  1818. intel_cpufreq_stop_cpu(policy);
  1819. }
  1820. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  1821. {
  1822. intel_pstate_exit_perf_limits(policy);
  1823. policy->fast_switch_possible = false;
  1824. return 0;
  1825. }
  1826. static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1827. {
  1828. struct cpudata *cpu;
  1829. int rc;
  1830. rc = intel_pstate_init_cpu(policy->cpu);
  1831. if (rc)
  1832. return rc;
  1833. cpu = all_cpu_data[policy->cpu];
  1834. /*
  1835. * We need sane value in the cpu->perf_limits, so inherit from global
  1836. * perf_limits limits, which are seeded with values based on the
  1837. * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up.
  1838. */
  1839. if (per_cpu_limits)
  1840. memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits));
  1841. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1842. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1843. /* cpuinfo and default policy values */
  1844. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1845. update_turbo_state();
  1846. policy->cpuinfo.max_freq = limits->turbo_disabled ?
  1847. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1848. policy->cpuinfo.max_freq *= cpu->pstate.scaling;
  1849. intel_pstate_init_acpi_perf_limits(policy);
  1850. cpumask_set_cpu(policy->cpu, policy->cpus);
  1851. policy->fast_switch_possible = true;
  1852. return 0;
  1853. }
  1854. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1855. {
  1856. int ret = __intel_pstate_cpu_init(policy);
  1857. if (ret)
  1858. return ret;
  1859. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  1860. if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
  1861. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1862. else
  1863. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1864. return 0;
  1865. }
  1866. static struct cpufreq_driver intel_pstate = {
  1867. .flags = CPUFREQ_CONST_LOOPS,
  1868. .verify = intel_pstate_verify_policy,
  1869. .setpolicy = intel_pstate_set_policy,
  1870. .suspend = intel_pstate_hwp_save_state,
  1871. .resume = intel_pstate_resume,
  1872. .get = intel_pstate_get,
  1873. .init = intel_pstate_cpu_init,
  1874. .exit = intel_pstate_cpu_exit,
  1875. .stop_cpu = intel_pstate_stop_cpu,
  1876. .name = "intel_pstate",
  1877. };
  1878. static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
  1879. {
  1880. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1881. struct perf_limits *perf_limits = limits;
  1882. update_turbo_state();
  1883. policy->cpuinfo.max_freq = limits->turbo_disabled ?
  1884. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  1885. cpufreq_verify_within_cpu_limits(policy);
  1886. if (per_cpu_limits)
  1887. perf_limits = cpu->perf_limits;
  1888. mutex_lock(&intel_pstate_limits_lock);
  1889. intel_pstate_update_perf_limits(policy, perf_limits);
  1890. mutex_unlock(&intel_pstate_limits_lock);
  1891. return 0;
  1892. }
  1893. static unsigned int intel_cpufreq_turbo_update(struct cpudata *cpu,
  1894. struct cpufreq_policy *policy,
  1895. unsigned int target_freq)
  1896. {
  1897. unsigned int max_freq;
  1898. update_turbo_state();
  1899. max_freq = limits->no_turbo || limits->turbo_disabled ?
  1900. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  1901. policy->cpuinfo.max_freq = max_freq;
  1902. if (policy->max > max_freq)
  1903. policy->max = max_freq;
  1904. if (target_freq > max_freq)
  1905. target_freq = max_freq;
  1906. return target_freq;
  1907. }
  1908. static int intel_cpufreq_target(struct cpufreq_policy *policy,
  1909. unsigned int target_freq,
  1910. unsigned int relation)
  1911. {
  1912. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1913. struct cpufreq_freqs freqs;
  1914. int target_pstate;
  1915. freqs.old = policy->cur;
  1916. freqs.new = intel_cpufreq_turbo_update(cpu, policy, target_freq);
  1917. cpufreq_freq_transition_begin(policy, &freqs);
  1918. switch (relation) {
  1919. case CPUFREQ_RELATION_L:
  1920. target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
  1921. break;
  1922. case CPUFREQ_RELATION_H:
  1923. target_pstate = freqs.new / cpu->pstate.scaling;
  1924. break;
  1925. default:
  1926. target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
  1927. break;
  1928. }
  1929. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  1930. if (target_pstate != cpu->pstate.current_pstate) {
  1931. cpu->pstate.current_pstate = target_pstate;
  1932. wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
  1933. pstate_funcs.get_val(cpu, target_pstate));
  1934. }
  1935. cpufreq_freq_transition_end(policy, &freqs, false);
  1936. return 0;
  1937. }
  1938. static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
  1939. unsigned int target_freq)
  1940. {
  1941. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1942. int target_pstate;
  1943. target_freq = intel_cpufreq_turbo_update(cpu, policy, target_freq);
  1944. target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
  1945. intel_pstate_update_pstate(cpu, target_pstate);
  1946. return target_freq;
  1947. }
  1948. static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
  1949. {
  1950. int ret = __intel_pstate_cpu_init(policy);
  1951. if (ret)
  1952. return ret;
  1953. policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
  1954. /* This reflects the intel_pstate_get_cpu_pstates() setting. */
  1955. policy->cur = policy->cpuinfo.min_freq;
  1956. return 0;
  1957. }
  1958. static struct cpufreq_driver intel_cpufreq = {
  1959. .flags = CPUFREQ_CONST_LOOPS,
  1960. .verify = intel_cpufreq_verify_policy,
  1961. .target = intel_cpufreq_target,
  1962. .fast_switch = intel_cpufreq_fast_switch,
  1963. .init = intel_cpufreq_cpu_init,
  1964. .exit = intel_pstate_cpu_exit,
  1965. .stop_cpu = intel_cpufreq_stop_cpu,
  1966. .name = "intel_cpufreq",
  1967. };
  1968. static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
  1969. static void intel_pstate_driver_cleanup(void)
  1970. {
  1971. unsigned int cpu;
  1972. get_online_cpus();
  1973. for_each_online_cpu(cpu) {
  1974. if (all_cpu_data[cpu]) {
  1975. if (intel_pstate_driver == &intel_pstate)
  1976. intel_pstate_clear_update_util_hook(cpu);
  1977. kfree(all_cpu_data[cpu]);
  1978. all_cpu_data[cpu] = NULL;
  1979. }
  1980. }
  1981. put_online_cpus();
  1982. }
  1983. static int intel_pstate_register_driver(void)
  1984. {
  1985. int ret;
  1986. ret = cpufreq_register_driver(intel_pstate_driver);
  1987. if (ret) {
  1988. intel_pstate_driver_cleanup();
  1989. return ret;
  1990. }
  1991. mutex_lock(&intel_pstate_limits_lock);
  1992. driver_registered = true;
  1993. mutex_unlock(&intel_pstate_limits_lock);
  1994. if (intel_pstate_driver == &intel_pstate && !hwp_active &&
  1995. pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
  1996. intel_pstate_debug_expose_params();
  1997. return 0;
  1998. }
  1999. static int intel_pstate_unregister_driver(void)
  2000. {
  2001. if (hwp_active)
  2002. return -EBUSY;
  2003. if (intel_pstate_driver == &intel_pstate && !hwp_active &&
  2004. pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
  2005. intel_pstate_debug_hide_params();
  2006. mutex_lock(&intel_pstate_limits_lock);
  2007. driver_registered = false;
  2008. mutex_unlock(&intel_pstate_limits_lock);
  2009. cpufreq_unregister_driver(intel_pstate_driver);
  2010. intel_pstate_driver_cleanup();
  2011. return 0;
  2012. }
  2013. static ssize_t intel_pstate_show_status(char *buf)
  2014. {
  2015. if (!driver_registered)
  2016. return sprintf(buf, "off\n");
  2017. return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
  2018. "active" : "passive");
  2019. }
  2020. static int intel_pstate_update_status(const char *buf, size_t size)
  2021. {
  2022. int ret;
  2023. if (size == 3 && !strncmp(buf, "off", size))
  2024. return driver_registered ?
  2025. intel_pstate_unregister_driver() : -EINVAL;
  2026. if (size == 6 && !strncmp(buf, "active", size)) {
  2027. if (driver_registered) {
  2028. if (intel_pstate_driver == &intel_pstate)
  2029. return 0;
  2030. ret = intel_pstate_unregister_driver();
  2031. if (ret)
  2032. return ret;
  2033. }
  2034. intel_pstate_driver = &intel_pstate;
  2035. return intel_pstate_register_driver();
  2036. }
  2037. if (size == 7 && !strncmp(buf, "passive", size)) {
  2038. if (driver_registered) {
  2039. if (intel_pstate_driver != &intel_pstate)
  2040. return 0;
  2041. ret = intel_pstate_unregister_driver();
  2042. if (ret)
  2043. return ret;
  2044. }
  2045. intel_pstate_driver = &intel_cpufreq;
  2046. return intel_pstate_register_driver();
  2047. }
  2048. return -EINVAL;
  2049. }
  2050. static int no_load __initdata;
  2051. static int no_hwp __initdata;
  2052. static int hwp_only __initdata;
  2053. static unsigned int force_load __initdata;
  2054. static int __init intel_pstate_msrs_not_valid(void)
  2055. {
  2056. if (!pstate_funcs.get_max() ||
  2057. !pstate_funcs.get_min() ||
  2058. !pstate_funcs.get_turbo())
  2059. return -ENODEV;
  2060. return 0;
  2061. }
  2062. static void __init copy_pid_params(struct pstate_adjust_policy *policy)
  2063. {
  2064. pid_params.sample_rate_ms = policy->sample_rate_ms;
  2065. pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
  2066. pid_params.p_gain_pct = policy->p_gain_pct;
  2067. pid_params.i_gain_pct = policy->i_gain_pct;
  2068. pid_params.d_gain_pct = policy->d_gain_pct;
  2069. pid_params.deadband = policy->deadband;
  2070. pid_params.setpoint = policy->setpoint;
  2071. }
  2072. #ifdef CONFIG_ACPI
  2073. static void intel_pstate_use_acpi_profile(void)
  2074. {
  2075. if (acpi_gbl_FADT.preferred_profile == PM_MOBILE)
  2076. pstate_funcs.get_target_pstate =
  2077. get_target_pstate_use_cpu_load;
  2078. }
  2079. #else
  2080. static void intel_pstate_use_acpi_profile(void)
  2081. {
  2082. }
  2083. #endif
  2084. static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
  2085. {
  2086. pstate_funcs.get_max = funcs->get_max;
  2087. pstate_funcs.get_max_physical = funcs->get_max_physical;
  2088. pstate_funcs.get_min = funcs->get_min;
  2089. pstate_funcs.get_turbo = funcs->get_turbo;
  2090. pstate_funcs.get_scaling = funcs->get_scaling;
  2091. pstate_funcs.get_val = funcs->get_val;
  2092. pstate_funcs.get_vid = funcs->get_vid;
  2093. pstate_funcs.get_target_pstate = funcs->get_target_pstate;
  2094. intel_pstate_use_acpi_profile();
  2095. }
  2096. #ifdef CONFIG_ACPI
  2097. static bool __init intel_pstate_no_acpi_pss(void)
  2098. {
  2099. int i;
  2100. for_each_possible_cpu(i) {
  2101. acpi_status status;
  2102. union acpi_object *pss;
  2103. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  2104. struct acpi_processor *pr = per_cpu(processors, i);
  2105. if (!pr)
  2106. continue;
  2107. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  2108. if (ACPI_FAILURE(status))
  2109. continue;
  2110. pss = buffer.pointer;
  2111. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  2112. kfree(pss);
  2113. return false;
  2114. }
  2115. kfree(pss);
  2116. }
  2117. return true;
  2118. }
  2119. static bool __init intel_pstate_has_acpi_ppc(void)
  2120. {
  2121. int i;
  2122. for_each_possible_cpu(i) {
  2123. struct acpi_processor *pr = per_cpu(processors, i);
  2124. if (!pr)
  2125. continue;
  2126. if (acpi_has_method(pr->handle, "_PPC"))
  2127. return true;
  2128. }
  2129. return false;
  2130. }
  2131. enum {
  2132. PSS,
  2133. PPC,
  2134. };
  2135. struct hw_vendor_info {
  2136. u16 valid;
  2137. char oem_id[ACPI_OEM_ID_SIZE];
  2138. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  2139. int oem_pwr_table;
  2140. };
  2141. /* Hardware vendor-specific info that has its own power management modes */
  2142. static struct hw_vendor_info vendor_info[] __initdata = {
  2143. {1, "HP ", "ProLiant", PSS},
  2144. {1, "ORACLE", "X4-2 ", PPC},
  2145. {1, "ORACLE", "X4-2L ", PPC},
  2146. {1, "ORACLE", "X4-2B ", PPC},
  2147. {1, "ORACLE", "X3-2 ", PPC},
  2148. {1, "ORACLE", "X3-2L ", PPC},
  2149. {1, "ORACLE", "X3-2B ", PPC},
  2150. {1, "ORACLE", "X4470M2 ", PPC},
  2151. {1, "ORACLE", "X4270M3 ", PPC},
  2152. {1, "ORACLE", "X4270M2 ", PPC},
  2153. {1, "ORACLE", "X4170M2 ", PPC},
  2154. {1, "ORACLE", "X4170 M3", PPC},
  2155. {1, "ORACLE", "X4275 M3", PPC},
  2156. {1, "ORACLE", "X6-2 ", PPC},
  2157. {1, "ORACLE", "Sudbury ", PPC},
  2158. {0, "", ""},
  2159. };
  2160. static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
  2161. {
  2162. struct acpi_table_header hdr;
  2163. struct hw_vendor_info *v_info;
  2164. const struct x86_cpu_id *id;
  2165. u64 misc_pwr;
  2166. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  2167. if (id) {
  2168. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  2169. if ( misc_pwr & (1 << 8))
  2170. return true;
  2171. }
  2172. if (acpi_disabled ||
  2173. ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
  2174. return false;
  2175. for (v_info = vendor_info; v_info->valid; v_info++) {
  2176. if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
  2177. !strncmp(hdr.oem_table_id, v_info->oem_table_id,
  2178. ACPI_OEM_TABLE_ID_SIZE))
  2179. switch (v_info->oem_pwr_table) {
  2180. case PSS:
  2181. return intel_pstate_no_acpi_pss();
  2182. case PPC:
  2183. return intel_pstate_has_acpi_ppc() &&
  2184. (!force_load);
  2185. }
  2186. }
  2187. return false;
  2188. }
  2189. static void intel_pstate_request_control_from_smm(void)
  2190. {
  2191. /*
  2192. * It may be unsafe to request P-states control from SMM if _PPC support
  2193. * has not been enabled.
  2194. */
  2195. if (acpi_ppc)
  2196. acpi_processor_pstate_control();
  2197. }
  2198. #else /* CONFIG_ACPI not enabled */
  2199. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  2200. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  2201. static inline void intel_pstate_request_control_from_smm(void) {}
  2202. #endif /* CONFIG_ACPI */
  2203. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  2204. { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
  2205. {}
  2206. };
  2207. static int __init intel_pstate_init(void)
  2208. {
  2209. const struct x86_cpu_id *id;
  2210. struct cpu_defaults *cpu_def;
  2211. int rc = 0;
  2212. if (no_load)
  2213. return -ENODEV;
  2214. if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
  2215. copy_cpu_funcs(&core_params.funcs);
  2216. hwp_active++;
  2217. intel_pstate.attr = hwp_cpufreq_attrs;
  2218. goto hwp_cpu_matched;
  2219. }
  2220. id = x86_match_cpu(intel_pstate_cpu_ids);
  2221. if (!id)
  2222. return -ENODEV;
  2223. cpu_def = (struct cpu_defaults *)id->driver_data;
  2224. copy_pid_params(&cpu_def->pid_policy);
  2225. copy_cpu_funcs(&cpu_def->funcs);
  2226. if (intel_pstate_msrs_not_valid())
  2227. return -ENODEV;
  2228. hwp_cpu_matched:
  2229. /*
  2230. * The Intel pstate driver will be ignored if the platform
  2231. * firmware has its own power management modes.
  2232. */
  2233. if (intel_pstate_platform_pwr_mgmt_exists())
  2234. return -ENODEV;
  2235. if (!hwp_active && hwp_only)
  2236. return -ENOTSUPP;
  2237. pr_info("Intel P-state driver initializing\n");
  2238. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  2239. if (!all_cpu_data)
  2240. return -ENOMEM;
  2241. intel_pstate_request_control_from_smm();
  2242. intel_pstate_sysfs_expose_params();
  2243. mutex_lock(&intel_pstate_driver_lock);
  2244. rc = intel_pstate_register_driver();
  2245. mutex_unlock(&intel_pstate_driver_lock);
  2246. if (rc)
  2247. return rc;
  2248. if (hwp_active)
  2249. pr_info("HWP enabled\n");
  2250. return 0;
  2251. }
  2252. device_initcall(intel_pstate_init);
  2253. static int __init intel_pstate_setup(char *str)
  2254. {
  2255. if (!str)
  2256. return -EINVAL;
  2257. if (!strcmp(str, "disable")) {
  2258. no_load = 1;
  2259. } else if (!strcmp(str, "passive")) {
  2260. pr_info("Passive mode enabled\n");
  2261. intel_pstate_driver = &intel_cpufreq;
  2262. no_hwp = 1;
  2263. }
  2264. if (!strcmp(str, "no_hwp")) {
  2265. pr_info("HWP disabled\n");
  2266. no_hwp = 1;
  2267. }
  2268. if (!strcmp(str, "force"))
  2269. force_load = 1;
  2270. if (!strcmp(str, "hwp_only"))
  2271. hwp_only = 1;
  2272. if (!strcmp(str, "per_cpu_perf_limits"))
  2273. per_cpu_limits = true;
  2274. #ifdef CONFIG_ACPI
  2275. if (!strcmp(str, "support_acpi_ppc"))
  2276. acpi_ppc = true;
  2277. #endif
  2278. return 0;
  2279. }
  2280. early_param("intel_pstate", intel_pstate_setup);
  2281. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  2282. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  2283. MODULE_LICENSE("GPL");