intel_pstate.c 69 KB

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