intel_pstate.c 70 KB

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