intel_pstate.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel_pstate.c: Native P state management for Intel processors
  4. *
  5. * (C) Copyright 2012 Intel Corporation
  6. * Author: Dirk Brandewie <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/kernel_stat.h>
  11. #include <linux/module.h>
  12. #include <linux/ktime.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/tick.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched/cpufreq.h>
  17. #include <linux/list.h>
  18. #include <linux/cpu.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/types.h>
  22. #include <linux/fs.h>
  23. #include <linux/acpi.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/pm_qos.h>
  26. #include <trace/events/power.h>
  27. #include <asm/cpu.h>
  28. #include <asm/div64.h>
  29. #include <asm/msr.h>
  30. #include <asm/cpu_device_id.h>
  31. #include <asm/cpufeature.h>
  32. #include <asm/intel-family.h>
  33. #include "../drivers/thermal/intel/thermal_interrupt.h"
  34. #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
  35. #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
  36. #define INTEL_CPUFREQ_TRANSITION_DELAY_HWP 5000
  37. #define INTEL_CPUFREQ_TRANSITION_DELAY 500
  38. #ifdef CONFIG_ACPI
  39. #include <acpi/processor.h>
  40. #include <acpi/cppc_acpi.h>
  41. #endif
  42. #define FRAC_BITS 8
  43. #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  44. #define fp_toint(X) ((X) >> FRAC_BITS)
  45. #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
  46. #define EXT_BITS 6
  47. #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
  48. #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
  49. #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
  50. static inline int32_t mul_fp(int32_t x, int32_t y)
  51. {
  52. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  53. }
  54. static inline int32_t div_fp(s64 x, s64 y)
  55. {
  56. return div64_s64((int64_t)x << FRAC_BITS, y);
  57. }
  58. static inline int ceiling_fp(int32_t x)
  59. {
  60. int mask, ret;
  61. ret = fp_toint(x);
  62. mask = (1 << FRAC_BITS) - 1;
  63. if (x & mask)
  64. ret += 1;
  65. return ret;
  66. }
  67. static inline u64 mul_ext_fp(u64 x, u64 y)
  68. {
  69. return (x * y) >> EXT_FRAC_BITS;
  70. }
  71. static inline u64 div_ext_fp(u64 x, u64 y)
  72. {
  73. return div64_u64(x << EXT_FRAC_BITS, y);
  74. }
  75. /**
  76. * struct sample - Store performance sample
  77. * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
  78. * performance during last sample period
  79. * @busy_scaled: Scaled busy value which is used to calculate next
  80. * P state. This can be different than core_avg_perf
  81. * to account for cpu idle period
  82. * @aperf: Difference of actual performance frequency clock count
  83. * read from APERF MSR between last and current sample
  84. * @mperf: Difference of maximum performance frequency clock count
  85. * read from MPERF MSR between last and current sample
  86. * @tsc: Difference of time stamp counter between last and
  87. * current sample
  88. * @time: Current time from scheduler
  89. *
  90. * This structure is used in the cpudata structure to store performance sample
  91. * data for choosing next P State.
  92. */
  93. struct sample {
  94. int32_t core_avg_perf;
  95. int32_t busy_scaled;
  96. u64 aperf;
  97. u64 mperf;
  98. u64 tsc;
  99. u64 time;
  100. };
  101. /**
  102. * struct pstate_data - Store P state data
  103. * @current_pstate: Current requested P state
  104. * @min_pstate: Min P state possible for this platform
  105. * @max_pstate: Max P state possible for this platform
  106. * @max_pstate_physical:This is physical Max P state for a processor
  107. * This can be higher than the max_pstate which can
  108. * be limited by platform thermal design power limits
  109. * @perf_ctl_scaling: PERF_CTL P-state to frequency scaling factor
  110. * @scaling: Scaling factor between performance and frequency
  111. * @turbo_pstate: Max Turbo P state possible for this platform
  112. * @min_freq: @min_pstate frequency in cpufreq units
  113. * @max_freq: @max_pstate frequency in cpufreq units
  114. * @turbo_freq: @turbo_pstate frequency in cpufreq units
  115. *
  116. * Stores the per cpu model P state limits and current P state.
  117. */
  118. struct pstate_data {
  119. int current_pstate;
  120. int min_pstate;
  121. int max_pstate;
  122. int max_pstate_physical;
  123. int perf_ctl_scaling;
  124. int scaling;
  125. int turbo_pstate;
  126. unsigned int min_freq;
  127. unsigned int max_freq;
  128. unsigned int turbo_freq;
  129. };
  130. /**
  131. * struct vid_data - Stores voltage information data
  132. * @min: VID data for this platform corresponding to
  133. * the lowest P state
  134. * @max: VID data corresponding to the highest P State.
  135. * @turbo: VID data for turbo P state
  136. * @ratio: Ratio of (vid max - vid min) /
  137. * (max P state - Min P State)
  138. *
  139. * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
  140. * This data is used in Atom platforms, where in addition to target P state,
  141. * the voltage data needs to be specified to select next P State.
  142. */
  143. struct vid_data {
  144. int min;
  145. int max;
  146. int turbo;
  147. int32_t ratio;
  148. };
  149. /**
  150. * struct global_params - Global parameters, mostly tunable via sysfs.
  151. * @no_turbo: Whether or not to use turbo P-states.
  152. * @turbo_disabled: Whether or not turbo P-states are available at all,
  153. * based on the MSR_IA32_MISC_ENABLE value and whether or
  154. * not the maximum reported turbo P-state is different from
  155. * the maximum reported non-turbo one.
  156. * @turbo_disabled_mf: The @turbo_disabled value reflected by cpuinfo.max_freq.
  157. * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
  158. * P-state capacity.
  159. * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
  160. * P-state capacity.
  161. */
  162. struct global_params {
  163. bool no_turbo;
  164. bool turbo_disabled;
  165. bool turbo_disabled_mf;
  166. int max_perf_pct;
  167. int min_perf_pct;
  168. };
  169. /**
  170. * struct cpudata - Per CPU instance data storage
  171. * @cpu: CPU number for this instance data
  172. * @policy: CPUFreq policy value
  173. * @update_util: CPUFreq utility callback information
  174. * @update_util_set: CPUFreq utility callback is set
  175. * @iowait_boost: iowait-related boost fraction
  176. * @last_update: Time of the last update.
  177. * @pstate: Stores P state limits for this CPU
  178. * @vid: Stores VID limits for this CPU
  179. * @last_sample_time: Last Sample time
  180. * @aperf_mperf_shift: APERF vs MPERF counting frequency difference
  181. * @prev_aperf: Last APERF value read from APERF MSR
  182. * @prev_mperf: Last MPERF value read from MPERF MSR
  183. * @prev_tsc: Last timestamp counter (TSC) value
  184. * @prev_cummulative_iowait: IO Wait time difference from last and
  185. * current sample
  186. * @sample: Storage for storing last Sample data
  187. * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios
  188. * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios
  189. * @acpi_perf_data: Stores ACPI perf information read from _PSS
  190. * @valid_pss_table: Set to true for valid ACPI _PSS entries found
  191. * @epp_powersave: Last saved HWP energy performance preference
  192. * (EPP) or energy performance bias (EPB),
  193. * when policy switched to performance
  194. * @epp_policy: Last saved policy used to set EPP/EPB
  195. * @epp_default: Power on default HWP energy performance
  196. * preference/bias
  197. * @epp_cached Cached HWP energy-performance preference value
  198. * @hwp_req_cached: Cached value of the last HWP Request MSR
  199. * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
  200. * @last_io_update: Last time when IO wake flag was set
  201. * @sched_flags: Store scheduler flags for possible cross CPU update
  202. * @hwp_boost_min: Last HWP boosted min performance
  203. * @suspended: Whether or not the driver has been suspended.
  204. * @hwp_notify_work: workqueue for HWP notifications.
  205. *
  206. * This structure stores per CPU instance data for all CPUs.
  207. */
  208. struct cpudata {
  209. int cpu;
  210. unsigned int policy;
  211. struct update_util_data update_util;
  212. bool update_util_set;
  213. struct pstate_data pstate;
  214. struct vid_data vid;
  215. u64 last_update;
  216. u64 last_sample_time;
  217. u64 aperf_mperf_shift;
  218. u64 prev_aperf;
  219. u64 prev_mperf;
  220. u64 prev_tsc;
  221. u64 prev_cummulative_iowait;
  222. struct sample sample;
  223. int32_t min_perf_ratio;
  224. int32_t max_perf_ratio;
  225. #ifdef CONFIG_ACPI
  226. struct acpi_processor_performance acpi_perf_data;
  227. bool valid_pss_table;
  228. #endif
  229. unsigned int iowait_boost;
  230. s16 epp_powersave;
  231. s16 epp_policy;
  232. s16 epp_default;
  233. s16 epp_cached;
  234. u64 hwp_req_cached;
  235. u64 hwp_cap_cached;
  236. u64 last_io_update;
  237. unsigned int sched_flags;
  238. u32 hwp_boost_min;
  239. bool suspended;
  240. struct delayed_work hwp_notify_work;
  241. };
  242. static struct cpudata **all_cpu_data;
  243. /**
  244. * struct pstate_funcs - Per CPU model specific callbacks
  245. * @get_max: Callback to get maximum non turbo effective P state
  246. * @get_max_physical: Callback to get maximum non turbo physical P state
  247. * @get_min: Callback to get minimum P state
  248. * @get_turbo: Callback to get turbo P state
  249. * @get_scaling: Callback to get frequency scaling factor
  250. * @get_cpu_scaling: Get frequency scaling factor for a given cpu
  251. * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference
  252. * @get_val: Callback to convert P state to actual MSR write value
  253. * @get_vid: Callback to get VID data for Atom platforms
  254. *
  255. * Core and Atom CPU models have different way to get P State limits. This
  256. * structure is used to store those callbacks.
  257. */
  258. struct pstate_funcs {
  259. int (*get_max)(int cpu);
  260. int (*get_max_physical)(int cpu);
  261. int (*get_min)(int cpu);
  262. int (*get_turbo)(int cpu);
  263. int (*get_scaling)(void);
  264. int (*get_cpu_scaling)(int cpu);
  265. int (*get_aperf_mperf_shift)(void);
  266. u64 (*get_val)(struct cpudata*, int pstate);
  267. void (*get_vid)(struct cpudata *);
  268. };
  269. static struct pstate_funcs pstate_funcs __read_mostly;
  270. static int hwp_active __read_mostly;
  271. static int hwp_mode_bdw __read_mostly;
  272. static bool per_cpu_limits __read_mostly;
  273. static bool hwp_boost __read_mostly;
  274. static struct cpufreq_driver *intel_pstate_driver __read_mostly;
  275. #ifdef CONFIG_ACPI
  276. static bool acpi_ppc;
  277. #endif
  278. static struct global_params global;
  279. static DEFINE_MUTEX(intel_pstate_driver_lock);
  280. static DEFINE_MUTEX(intel_pstate_limits_lock);
  281. #ifdef CONFIG_ACPI
  282. static bool intel_pstate_acpi_pm_profile_server(void)
  283. {
  284. if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
  285. acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
  286. return true;
  287. return false;
  288. }
  289. static bool intel_pstate_get_ppc_enable_status(void)
  290. {
  291. if (intel_pstate_acpi_pm_profile_server())
  292. return true;
  293. return acpi_ppc;
  294. }
  295. #ifdef CONFIG_ACPI_CPPC_LIB
  296. /* The work item is needed to avoid CPU hotplug locking issues */
  297. static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
  298. {
  299. sched_set_itmt_support();
  300. }
  301. static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
  302. #define CPPC_MAX_PERF U8_MAX
  303. static void intel_pstate_set_itmt_prio(int cpu)
  304. {
  305. struct cppc_perf_caps cppc_perf;
  306. static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
  307. int ret;
  308. ret = cppc_get_perf_caps(cpu, &cppc_perf);
  309. if (ret)
  310. return;
  311. /*
  312. * On some systems with overclocking enabled, CPPC.highest_perf is hardcoded to 0xff.
  313. * In this case we can't use CPPC.highest_perf to enable ITMT.
  314. * In this case we can look at MSR_HWP_CAPABILITIES bits [8:0] to decide.
  315. */
  316. if (cppc_perf.highest_perf == CPPC_MAX_PERF)
  317. cppc_perf.highest_perf = HWP_HIGHEST_PERF(READ_ONCE(all_cpu_data[cpu]->hwp_cap_cached));
  318. /*
  319. * The priorities can be set regardless of whether or not
  320. * sched_set_itmt_support(true) has been called and it is valid to
  321. * update them at any time after it has been called.
  322. */
  323. sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
  324. if (max_highest_perf <= min_highest_perf) {
  325. if (cppc_perf.highest_perf > max_highest_perf)
  326. max_highest_perf = cppc_perf.highest_perf;
  327. if (cppc_perf.highest_perf < min_highest_perf)
  328. min_highest_perf = cppc_perf.highest_perf;
  329. if (max_highest_perf > min_highest_perf) {
  330. /*
  331. * This code can be run during CPU online under the
  332. * CPU hotplug locks, so sched_set_itmt_support()
  333. * cannot be called from here. Queue up a work item
  334. * to invoke it.
  335. */
  336. schedule_work(&sched_itmt_work);
  337. }
  338. }
  339. }
  340. static int intel_pstate_get_cppc_guaranteed(int cpu)
  341. {
  342. struct cppc_perf_caps cppc_perf;
  343. int ret;
  344. ret = cppc_get_perf_caps(cpu, &cppc_perf);
  345. if (ret)
  346. return ret;
  347. if (cppc_perf.guaranteed_perf)
  348. return cppc_perf.guaranteed_perf;
  349. return cppc_perf.nominal_perf;
  350. }
  351. #else /* CONFIG_ACPI_CPPC_LIB */
  352. static inline void intel_pstate_set_itmt_prio(int cpu)
  353. {
  354. }
  355. #endif /* CONFIG_ACPI_CPPC_LIB */
  356. static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  357. {
  358. struct cpudata *cpu;
  359. int ret;
  360. int i;
  361. if (hwp_active) {
  362. intel_pstate_set_itmt_prio(policy->cpu);
  363. return;
  364. }
  365. if (!intel_pstate_get_ppc_enable_status())
  366. return;
  367. cpu = all_cpu_data[policy->cpu];
  368. ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
  369. policy->cpu);
  370. if (ret)
  371. return;
  372. /*
  373. * Check if the control value in _PSS is for PERF_CTL MSR, which should
  374. * guarantee that the states returned by it map to the states in our
  375. * list directly.
  376. */
  377. if (cpu->acpi_perf_data.control_register.space_id !=
  378. ACPI_ADR_SPACE_FIXED_HARDWARE)
  379. goto err;
  380. /*
  381. * If there is only one entry _PSS, simply ignore _PSS and continue as
  382. * usual without taking _PSS into account
  383. */
  384. if (cpu->acpi_perf_data.state_count < 2)
  385. goto err;
  386. pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
  387. for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
  388. pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
  389. (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
  390. (u32) cpu->acpi_perf_data.states[i].core_frequency,
  391. (u32) cpu->acpi_perf_data.states[i].power,
  392. (u32) cpu->acpi_perf_data.states[i].control);
  393. }
  394. cpu->valid_pss_table = true;
  395. pr_debug("_PPC limits will be enforced\n");
  396. return;
  397. err:
  398. cpu->valid_pss_table = false;
  399. acpi_processor_unregister_performance(policy->cpu);
  400. }
  401. static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  402. {
  403. struct cpudata *cpu;
  404. cpu = all_cpu_data[policy->cpu];
  405. if (!cpu->valid_pss_table)
  406. return;
  407. acpi_processor_unregister_performance(policy->cpu);
  408. }
  409. #else /* CONFIG_ACPI */
  410. static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  411. {
  412. }
  413. static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  414. {
  415. }
  416. static inline bool intel_pstate_acpi_pm_profile_server(void)
  417. {
  418. return false;
  419. }
  420. #endif /* CONFIG_ACPI */
  421. #ifndef CONFIG_ACPI_CPPC_LIB
  422. static inline int intel_pstate_get_cppc_guaranteed(int cpu)
  423. {
  424. return -ENOTSUPP;
  425. }
  426. #endif /* CONFIG_ACPI_CPPC_LIB */
  427. /**
  428. * intel_pstate_hybrid_hwp_adjust - Calibrate HWP performance levels.
  429. * @cpu: Target CPU.
  430. *
  431. * On hybrid processors, HWP may expose more performance levels than there are
  432. * P-states accessible through the PERF_CTL interface. If that happens, the
  433. * scaling factor between HWP performance levels and CPU frequency will be less
  434. * than the scaling factor between P-state values and CPU frequency.
  435. *
  436. * In that case, adjust the CPU parameters used in computations accordingly.
  437. */
  438. static void intel_pstate_hybrid_hwp_adjust(struct cpudata *cpu)
  439. {
  440. int perf_ctl_max_phys = cpu->pstate.max_pstate_physical;
  441. int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
  442. int perf_ctl_turbo = pstate_funcs.get_turbo(cpu->cpu);
  443. int scaling = cpu->pstate.scaling;
  444. pr_debug("CPU%d: perf_ctl_max_phys = %d\n", cpu->cpu, perf_ctl_max_phys);
  445. pr_debug("CPU%d: perf_ctl_turbo = %d\n", cpu->cpu, perf_ctl_turbo);
  446. pr_debug("CPU%d: perf_ctl_scaling = %d\n", cpu->cpu, perf_ctl_scaling);
  447. pr_debug("CPU%d: HWP_CAP guaranteed = %d\n", cpu->cpu, cpu->pstate.max_pstate);
  448. pr_debug("CPU%d: HWP_CAP highest = %d\n", cpu->cpu, cpu->pstate.turbo_pstate);
  449. pr_debug("CPU%d: HWP-to-frequency scaling factor: %d\n", cpu->cpu, scaling);
  450. cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_pstate * scaling,
  451. perf_ctl_scaling);
  452. cpu->pstate.max_freq = rounddown(cpu->pstate.max_pstate * scaling,
  453. perf_ctl_scaling);
  454. cpu->pstate.max_pstate_physical =
  455. DIV_ROUND_UP(perf_ctl_max_phys * perf_ctl_scaling,
  456. scaling);
  457. cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling;
  458. /*
  459. * Cast the min P-state value retrieved via pstate_funcs.get_min() to
  460. * the effective range of HWP performance levels.
  461. */
  462. cpu->pstate.min_pstate = DIV_ROUND_UP(cpu->pstate.min_freq, scaling);
  463. }
  464. static inline void update_turbo_state(void)
  465. {
  466. u64 misc_en;
  467. struct cpudata *cpu;
  468. cpu = all_cpu_data[0];
  469. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  470. global.turbo_disabled =
  471. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  472. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  473. }
  474. static int min_perf_pct_min(void)
  475. {
  476. struct cpudata *cpu = all_cpu_data[0];
  477. int turbo_pstate = cpu->pstate.turbo_pstate;
  478. return turbo_pstate ?
  479. (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
  480. }
  481. static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
  482. {
  483. u64 epb;
  484. int ret;
  485. if (!boot_cpu_has(X86_FEATURE_EPB))
  486. return -ENXIO;
  487. ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  488. if (ret)
  489. return (s16)ret;
  490. return (s16)(epb & 0x0f);
  491. }
  492. static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
  493. {
  494. s16 epp;
  495. if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
  496. /*
  497. * When hwp_req_data is 0, means that caller didn't read
  498. * MSR_HWP_REQUEST, so need to read and get EPP.
  499. */
  500. if (!hwp_req_data) {
  501. epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
  502. &hwp_req_data);
  503. if (epp)
  504. return epp;
  505. }
  506. epp = (hwp_req_data >> 24) & 0xff;
  507. } else {
  508. /* When there is no EPP present, HWP uses EPB settings */
  509. epp = intel_pstate_get_epb(cpu_data);
  510. }
  511. return epp;
  512. }
  513. static int intel_pstate_set_epb(int cpu, s16 pref)
  514. {
  515. u64 epb;
  516. int ret;
  517. if (!boot_cpu_has(X86_FEATURE_EPB))
  518. return -ENXIO;
  519. ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  520. if (ret)
  521. return ret;
  522. epb = (epb & ~0x0f) | pref;
  523. wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
  524. return 0;
  525. }
  526. /*
  527. * EPP/EPB display strings corresponding to EPP index in the
  528. * energy_perf_strings[]
  529. * index String
  530. *-------------------------------------
  531. * 0 default
  532. * 1 performance
  533. * 2 balance_performance
  534. * 3 balance_power
  535. * 4 power
  536. */
  537. enum energy_perf_value_index {
  538. EPP_INDEX_DEFAULT = 0,
  539. EPP_INDEX_PERFORMANCE,
  540. EPP_INDEX_BALANCE_PERFORMANCE,
  541. EPP_INDEX_BALANCE_POWERSAVE,
  542. EPP_INDEX_POWERSAVE,
  543. };
  544. static const char * const energy_perf_strings[] = {
  545. [EPP_INDEX_DEFAULT] = "default",
  546. [EPP_INDEX_PERFORMANCE] = "performance",
  547. [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
  548. [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
  549. [EPP_INDEX_POWERSAVE] = "power",
  550. NULL
  551. };
  552. static unsigned int epp_values[] = {
  553. [EPP_INDEX_DEFAULT] = 0, /* Unused index */
  554. [EPP_INDEX_PERFORMANCE] = HWP_EPP_PERFORMANCE,
  555. [EPP_INDEX_BALANCE_PERFORMANCE] = HWP_EPP_BALANCE_PERFORMANCE,
  556. [EPP_INDEX_BALANCE_POWERSAVE] = HWP_EPP_BALANCE_POWERSAVE,
  557. [EPP_INDEX_POWERSAVE] = HWP_EPP_POWERSAVE,
  558. };
  559. static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
  560. {
  561. s16 epp;
  562. int index = -EINVAL;
  563. *raw_epp = 0;
  564. epp = intel_pstate_get_epp(cpu_data, 0);
  565. if (epp < 0)
  566. return epp;
  567. if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
  568. if (epp == epp_values[EPP_INDEX_PERFORMANCE])
  569. return EPP_INDEX_PERFORMANCE;
  570. if (epp == epp_values[EPP_INDEX_BALANCE_PERFORMANCE])
  571. return EPP_INDEX_BALANCE_PERFORMANCE;
  572. if (epp == epp_values[EPP_INDEX_BALANCE_POWERSAVE])
  573. return EPP_INDEX_BALANCE_POWERSAVE;
  574. if (epp == epp_values[EPP_INDEX_POWERSAVE])
  575. return EPP_INDEX_POWERSAVE;
  576. *raw_epp = epp;
  577. return 0;
  578. } else if (boot_cpu_has(X86_FEATURE_EPB)) {
  579. /*
  580. * Range:
  581. * 0x00-0x03 : Performance
  582. * 0x04-0x07 : Balance performance
  583. * 0x08-0x0B : Balance power
  584. * 0x0C-0x0F : Power
  585. * The EPB is a 4 bit value, but our ranges restrict the
  586. * value which can be set. Here only using top two bits
  587. * effectively.
  588. */
  589. index = (epp >> 2) + 1;
  590. }
  591. return index;
  592. }
  593. static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
  594. {
  595. int ret;
  596. /*
  597. * Use the cached HWP Request MSR value, because in the active mode the
  598. * register itself may be updated by intel_pstate_hwp_boost_up() or
  599. * intel_pstate_hwp_boost_down() at any time.
  600. */
  601. u64 value = READ_ONCE(cpu->hwp_req_cached);
  602. value &= ~GENMASK_ULL(31, 24);
  603. value |= (u64)epp << 24;
  604. /*
  605. * The only other updater of hwp_req_cached in the active mode,
  606. * intel_pstate_hwp_set(), is called under the same lock as this
  607. * function, so it cannot run in parallel with the update below.
  608. */
  609. WRITE_ONCE(cpu->hwp_req_cached, value);
  610. ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
  611. if (!ret)
  612. cpu->epp_cached = epp;
  613. return ret;
  614. }
  615. static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
  616. int pref_index, bool use_raw,
  617. u32 raw_epp)
  618. {
  619. int epp = -EINVAL;
  620. int ret;
  621. if (!pref_index)
  622. epp = cpu_data->epp_default;
  623. if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
  624. if (use_raw)
  625. epp = raw_epp;
  626. else if (epp == -EINVAL)
  627. epp = epp_values[pref_index];
  628. /*
  629. * To avoid confusion, refuse to set EPP to any values different
  630. * from 0 (performance) if the current policy is "performance",
  631. * because those values would be overridden.
  632. */
  633. if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
  634. return -EBUSY;
  635. ret = intel_pstate_set_epp(cpu_data, epp);
  636. } else {
  637. if (epp == -EINVAL)
  638. epp = (pref_index - 1) << 2;
  639. ret = intel_pstate_set_epb(cpu_data->cpu, epp);
  640. }
  641. return ret;
  642. }
  643. static ssize_t show_energy_performance_available_preferences(
  644. struct cpufreq_policy *policy, char *buf)
  645. {
  646. int i = 0;
  647. int ret = 0;
  648. while (energy_perf_strings[i] != NULL)
  649. ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
  650. ret += sprintf(&buf[ret], "\n");
  651. return ret;
  652. }
  653. cpufreq_freq_attr_ro(energy_performance_available_preferences);
  654. static struct cpufreq_driver intel_pstate;
  655. static ssize_t store_energy_performance_preference(
  656. struct cpufreq_policy *policy, const char *buf, size_t count)
  657. {
  658. struct cpudata *cpu = all_cpu_data[policy->cpu];
  659. char str_preference[21];
  660. bool raw = false;
  661. ssize_t ret;
  662. u32 epp = 0;
  663. ret = sscanf(buf, "%20s", str_preference);
  664. if (ret != 1)
  665. return -EINVAL;
  666. ret = match_string(energy_perf_strings, -1, str_preference);
  667. if (ret < 0) {
  668. if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
  669. return ret;
  670. ret = kstrtouint(buf, 10, &epp);
  671. if (ret)
  672. return ret;
  673. if (epp > 255)
  674. return -EINVAL;
  675. raw = true;
  676. }
  677. /*
  678. * This function runs with the policy R/W semaphore held, which
  679. * guarantees that the driver pointer will not change while it is
  680. * running.
  681. */
  682. if (!intel_pstate_driver)
  683. return -EAGAIN;
  684. mutex_lock(&intel_pstate_limits_lock);
  685. if (intel_pstate_driver == &intel_pstate) {
  686. ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
  687. } else {
  688. /*
  689. * In the passive mode the governor needs to be stopped on the
  690. * target CPU before the EPP update and restarted after it,
  691. * which is super-heavy-weight, so make sure it is worth doing
  692. * upfront.
  693. */
  694. if (!raw)
  695. epp = ret ? epp_values[ret] : cpu->epp_default;
  696. if (cpu->epp_cached != epp) {
  697. int err;
  698. cpufreq_stop_governor(policy);
  699. ret = intel_pstate_set_epp(cpu, epp);
  700. err = cpufreq_start_governor(policy);
  701. if (!ret)
  702. ret = err;
  703. } else {
  704. ret = 0;
  705. }
  706. }
  707. mutex_unlock(&intel_pstate_limits_lock);
  708. return ret ?: count;
  709. }
  710. static ssize_t show_energy_performance_preference(
  711. struct cpufreq_policy *policy, char *buf)
  712. {
  713. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  714. int preference, raw_epp;
  715. preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
  716. if (preference < 0)
  717. return preference;
  718. if (raw_epp)
  719. return sprintf(buf, "%d\n", raw_epp);
  720. else
  721. return sprintf(buf, "%s\n", energy_perf_strings[preference]);
  722. }
  723. cpufreq_freq_attr_rw(energy_performance_preference);
  724. static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
  725. {
  726. struct cpudata *cpu = all_cpu_data[policy->cpu];
  727. int ratio, freq;
  728. ratio = intel_pstate_get_cppc_guaranteed(policy->cpu);
  729. if (ratio <= 0) {
  730. u64 cap;
  731. rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
  732. ratio = HWP_GUARANTEED_PERF(cap);
  733. }
  734. freq = ratio * cpu->pstate.scaling;
  735. if (cpu->pstate.scaling != cpu->pstate.perf_ctl_scaling)
  736. freq = rounddown(freq, cpu->pstate.perf_ctl_scaling);
  737. return sprintf(buf, "%d\n", freq);
  738. }
  739. cpufreq_freq_attr_ro(base_frequency);
  740. static struct freq_attr *hwp_cpufreq_attrs[] = {
  741. &energy_performance_preference,
  742. &energy_performance_available_preferences,
  743. &base_frequency,
  744. NULL,
  745. };
  746. static void __intel_pstate_get_hwp_cap(struct cpudata *cpu)
  747. {
  748. u64 cap;
  749. rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
  750. WRITE_ONCE(cpu->hwp_cap_cached, cap);
  751. cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(cap);
  752. cpu->pstate.turbo_pstate = HWP_HIGHEST_PERF(cap);
  753. }
  754. static void intel_pstate_get_hwp_cap(struct cpudata *cpu)
  755. {
  756. int scaling = cpu->pstate.scaling;
  757. __intel_pstate_get_hwp_cap(cpu);
  758. cpu->pstate.max_freq = cpu->pstate.max_pstate * scaling;
  759. cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * scaling;
  760. if (scaling != cpu->pstate.perf_ctl_scaling) {
  761. int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
  762. cpu->pstate.max_freq = rounddown(cpu->pstate.max_freq,
  763. perf_ctl_scaling);
  764. cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_freq,
  765. perf_ctl_scaling);
  766. }
  767. }
  768. static void intel_pstate_hwp_set(unsigned int cpu)
  769. {
  770. struct cpudata *cpu_data = all_cpu_data[cpu];
  771. int max, min;
  772. u64 value;
  773. s16 epp;
  774. max = cpu_data->max_perf_ratio;
  775. min = cpu_data->min_perf_ratio;
  776. if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
  777. min = max;
  778. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  779. value &= ~HWP_MIN_PERF(~0L);
  780. value |= HWP_MIN_PERF(min);
  781. value &= ~HWP_MAX_PERF(~0L);
  782. value |= HWP_MAX_PERF(max);
  783. if (cpu_data->epp_policy == cpu_data->policy)
  784. goto skip_epp;
  785. cpu_data->epp_policy = cpu_data->policy;
  786. if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
  787. epp = intel_pstate_get_epp(cpu_data, value);
  788. cpu_data->epp_powersave = epp;
  789. /* If EPP read was failed, then don't try to write */
  790. if (epp < 0)
  791. goto skip_epp;
  792. epp = 0;
  793. } else {
  794. /* skip setting EPP, when saved value is invalid */
  795. if (cpu_data->epp_powersave < 0)
  796. goto skip_epp;
  797. /*
  798. * No need to restore EPP when it is not zero. This
  799. * means:
  800. * - Policy is not changed
  801. * - user has manually changed
  802. * - Error reading EPB
  803. */
  804. epp = intel_pstate_get_epp(cpu_data, value);
  805. if (epp)
  806. goto skip_epp;
  807. epp = cpu_data->epp_powersave;
  808. }
  809. if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
  810. value &= ~GENMASK_ULL(31, 24);
  811. value |= (u64)epp << 24;
  812. } else {
  813. intel_pstate_set_epb(cpu, epp);
  814. }
  815. skip_epp:
  816. WRITE_ONCE(cpu_data->hwp_req_cached, value);
  817. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  818. }
  819. static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata);
  820. static void intel_pstate_hwp_offline(struct cpudata *cpu)
  821. {
  822. u64 value = READ_ONCE(cpu->hwp_req_cached);
  823. int min_perf;
  824. intel_pstate_disable_hwp_interrupt(cpu);
  825. if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
  826. /*
  827. * In case the EPP has been set to "performance" by the
  828. * active mode "performance" scaling algorithm, replace that
  829. * temporary value with the cached EPP one.
  830. */
  831. value &= ~GENMASK_ULL(31, 24);
  832. value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
  833. /*
  834. * However, make sure that EPP will be set to "performance" when
  835. * the CPU is brought back online again and the "performance"
  836. * scaling algorithm is still in effect.
  837. */
  838. cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN;
  839. }
  840. /*
  841. * Clear the desired perf field in the cached HWP request value to
  842. * prevent nonzero desired values from being leaked into the active
  843. * mode.
  844. */
  845. value &= ~HWP_DESIRED_PERF(~0L);
  846. WRITE_ONCE(cpu->hwp_req_cached, value);
  847. value &= ~GENMASK_ULL(31, 0);
  848. min_perf = HWP_LOWEST_PERF(READ_ONCE(cpu->hwp_cap_cached));
  849. /* Set hwp_max = hwp_min */
  850. value |= HWP_MAX_PERF(min_perf);
  851. value |= HWP_MIN_PERF(min_perf);
  852. /* Set EPP to min */
  853. if (boot_cpu_has(X86_FEATURE_HWP_EPP))
  854. value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
  855. wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
  856. }
  857. #define POWER_CTL_EE_ENABLE 1
  858. #define POWER_CTL_EE_DISABLE 2
  859. static int power_ctl_ee_state;
  860. static void set_power_ctl_ee_state(bool input)
  861. {
  862. u64 power_ctl;
  863. mutex_lock(&intel_pstate_driver_lock);
  864. rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
  865. if (input) {
  866. power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
  867. power_ctl_ee_state = POWER_CTL_EE_ENABLE;
  868. } else {
  869. power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
  870. power_ctl_ee_state = POWER_CTL_EE_DISABLE;
  871. }
  872. wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
  873. mutex_unlock(&intel_pstate_driver_lock);
  874. }
  875. static void intel_pstate_hwp_enable(struct cpudata *cpudata);
  876. static void intel_pstate_hwp_reenable(struct cpudata *cpu)
  877. {
  878. intel_pstate_hwp_enable(cpu);
  879. wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
  880. }
  881. static int intel_pstate_suspend(struct cpufreq_policy *policy)
  882. {
  883. struct cpudata *cpu = all_cpu_data[policy->cpu];
  884. pr_debug("CPU %d suspending\n", cpu->cpu);
  885. cpu->suspended = true;
  886. /* disable HWP interrupt and cancel any pending work */
  887. intel_pstate_disable_hwp_interrupt(cpu);
  888. return 0;
  889. }
  890. static int intel_pstate_resume(struct cpufreq_policy *policy)
  891. {
  892. struct cpudata *cpu = all_cpu_data[policy->cpu];
  893. pr_debug("CPU %d resuming\n", cpu->cpu);
  894. /* Only restore if the system default is changed */
  895. if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
  896. set_power_ctl_ee_state(true);
  897. else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
  898. set_power_ctl_ee_state(false);
  899. if (cpu->suspended && hwp_active) {
  900. mutex_lock(&intel_pstate_limits_lock);
  901. /* Re-enable HWP, because "online" has not done that. */
  902. intel_pstate_hwp_reenable(cpu);
  903. mutex_unlock(&intel_pstate_limits_lock);
  904. }
  905. cpu->suspended = false;
  906. return 0;
  907. }
  908. static void intel_pstate_update_policies(void)
  909. {
  910. int cpu;
  911. for_each_possible_cpu(cpu)
  912. cpufreq_update_policy(cpu);
  913. }
  914. static void __intel_pstate_update_max_freq(struct cpudata *cpudata,
  915. struct cpufreq_policy *policy)
  916. {
  917. policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
  918. cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
  919. refresh_frequency_limits(policy);
  920. }
  921. static void intel_pstate_update_max_freq(unsigned int cpu)
  922. {
  923. struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
  924. if (!policy)
  925. return;
  926. __intel_pstate_update_max_freq(all_cpu_data[cpu], policy);
  927. cpufreq_cpu_release(policy);
  928. }
  929. static void intel_pstate_update_limits(unsigned int cpu)
  930. {
  931. mutex_lock(&intel_pstate_driver_lock);
  932. update_turbo_state();
  933. /*
  934. * If turbo has been turned on or off globally, policy limits for
  935. * all CPUs need to be updated to reflect that.
  936. */
  937. if (global.turbo_disabled_mf != global.turbo_disabled) {
  938. global.turbo_disabled_mf = global.turbo_disabled;
  939. arch_set_max_freq_ratio(global.turbo_disabled);
  940. for_each_possible_cpu(cpu)
  941. intel_pstate_update_max_freq(cpu);
  942. } else {
  943. cpufreq_update_policy(cpu);
  944. }
  945. mutex_unlock(&intel_pstate_driver_lock);
  946. }
  947. /************************** sysfs begin ************************/
  948. #define show_one(file_name, object) \
  949. static ssize_t show_##file_name \
  950. (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
  951. { \
  952. return sprintf(buf, "%u\n", global.object); \
  953. }
  954. static ssize_t intel_pstate_show_status(char *buf);
  955. static int intel_pstate_update_status(const char *buf, size_t size);
  956. static ssize_t show_status(struct kobject *kobj,
  957. struct kobj_attribute *attr, char *buf)
  958. {
  959. ssize_t ret;
  960. mutex_lock(&intel_pstate_driver_lock);
  961. ret = intel_pstate_show_status(buf);
  962. mutex_unlock(&intel_pstate_driver_lock);
  963. return ret;
  964. }
  965. static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
  966. const char *buf, size_t count)
  967. {
  968. char *p = memchr(buf, '\n', count);
  969. int ret;
  970. mutex_lock(&intel_pstate_driver_lock);
  971. ret = intel_pstate_update_status(buf, p ? p - buf : count);
  972. mutex_unlock(&intel_pstate_driver_lock);
  973. return ret < 0 ? ret : count;
  974. }
  975. static ssize_t show_turbo_pct(struct kobject *kobj,
  976. struct kobj_attribute *attr, char *buf)
  977. {
  978. struct cpudata *cpu;
  979. int total, no_turbo, turbo_pct;
  980. uint32_t turbo_fp;
  981. mutex_lock(&intel_pstate_driver_lock);
  982. if (!intel_pstate_driver) {
  983. mutex_unlock(&intel_pstate_driver_lock);
  984. return -EAGAIN;
  985. }
  986. cpu = all_cpu_data[0];
  987. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  988. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  989. turbo_fp = div_fp(no_turbo, total);
  990. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  991. mutex_unlock(&intel_pstate_driver_lock);
  992. return sprintf(buf, "%u\n", turbo_pct);
  993. }
  994. static ssize_t show_num_pstates(struct kobject *kobj,
  995. struct kobj_attribute *attr, char *buf)
  996. {
  997. struct cpudata *cpu;
  998. int total;
  999. mutex_lock(&intel_pstate_driver_lock);
  1000. if (!intel_pstate_driver) {
  1001. mutex_unlock(&intel_pstate_driver_lock);
  1002. return -EAGAIN;
  1003. }
  1004. cpu = all_cpu_data[0];
  1005. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  1006. mutex_unlock(&intel_pstate_driver_lock);
  1007. return sprintf(buf, "%u\n", total);
  1008. }
  1009. static ssize_t show_no_turbo(struct kobject *kobj,
  1010. struct kobj_attribute *attr, char *buf)
  1011. {
  1012. ssize_t ret;
  1013. mutex_lock(&intel_pstate_driver_lock);
  1014. if (!intel_pstate_driver) {
  1015. mutex_unlock(&intel_pstate_driver_lock);
  1016. return -EAGAIN;
  1017. }
  1018. update_turbo_state();
  1019. if (global.turbo_disabled)
  1020. ret = sprintf(buf, "%u\n", global.turbo_disabled);
  1021. else
  1022. ret = sprintf(buf, "%u\n", global.no_turbo);
  1023. mutex_unlock(&intel_pstate_driver_lock);
  1024. return ret;
  1025. }
  1026. static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
  1027. const char *buf, size_t count)
  1028. {
  1029. unsigned int input;
  1030. int ret;
  1031. ret = sscanf(buf, "%u", &input);
  1032. if (ret != 1)
  1033. return -EINVAL;
  1034. mutex_lock(&intel_pstate_driver_lock);
  1035. if (!intel_pstate_driver) {
  1036. mutex_unlock(&intel_pstate_driver_lock);
  1037. return -EAGAIN;
  1038. }
  1039. mutex_lock(&intel_pstate_limits_lock);
  1040. update_turbo_state();
  1041. if (global.turbo_disabled) {
  1042. pr_notice_once("Turbo disabled by BIOS or unavailable on processor\n");
  1043. mutex_unlock(&intel_pstate_limits_lock);
  1044. mutex_unlock(&intel_pstate_driver_lock);
  1045. return -EPERM;
  1046. }
  1047. global.no_turbo = clamp_t(int, input, 0, 1);
  1048. if (global.no_turbo) {
  1049. struct cpudata *cpu = all_cpu_data[0];
  1050. int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
  1051. /* Squash the global minimum into the permitted range. */
  1052. if (global.min_perf_pct > pct)
  1053. global.min_perf_pct = pct;
  1054. }
  1055. mutex_unlock(&intel_pstate_limits_lock);
  1056. intel_pstate_update_policies();
  1057. arch_set_max_freq_ratio(global.no_turbo);
  1058. mutex_unlock(&intel_pstate_driver_lock);
  1059. return count;
  1060. }
  1061. static void update_qos_request(enum freq_qos_req_type type)
  1062. {
  1063. struct freq_qos_request *req;
  1064. struct cpufreq_policy *policy;
  1065. int i;
  1066. for_each_possible_cpu(i) {
  1067. struct cpudata *cpu = all_cpu_data[i];
  1068. unsigned int freq, perf_pct;
  1069. policy = cpufreq_cpu_get(i);
  1070. if (!policy)
  1071. continue;
  1072. req = policy->driver_data;
  1073. cpufreq_cpu_put(policy);
  1074. if (!req)
  1075. continue;
  1076. if (hwp_active)
  1077. intel_pstate_get_hwp_cap(cpu);
  1078. if (type == FREQ_QOS_MIN) {
  1079. perf_pct = global.min_perf_pct;
  1080. } else {
  1081. req++;
  1082. perf_pct = global.max_perf_pct;
  1083. }
  1084. freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * perf_pct, 100);
  1085. if (freq_qos_update_request(req, freq) < 0)
  1086. pr_warn("Failed to update freq constraint: CPU%d\n", i);
  1087. }
  1088. }
  1089. static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
  1090. const char *buf, size_t count)
  1091. {
  1092. unsigned int input;
  1093. int ret;
  1094. ret = sscanf(buf, "%u", &input);
  1095. if (ret != 1)
  1096. return -EINVAL;
  1097. mutex_lock(&intel_pstate_driver_lock);
  1098. if (!intel_pstate_driver) {
  1099. mutex_unlock(&intel_pstate_driver_lock);
  1100. return -EAGAIN;
  1101. }
  1102. mutex_lock(&intel_pstate_limits_lock);
  1103. global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
  1104. mutex_unlock(&intel_pstate_limits_lock);
  1105. if (intel_pstate_driver == &intel_pstate)
  1106. intel_pstate_update_policies();
  1107. else
  1108. update_qos_request(FREQ_QOS_MAX);
  1109. mutex_unlock(&intel_pstate_driver_lock);
  1110. return count;
  1111. }
  1112. static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
  1113. const char *buf, size_t count)
  1114. {
  1115. unsigned int input;
  1116. int ret;
  1117. ret = sscanf(buf, "%u", &input);
  1118. if (ret != 1)
  1119. return -EINVAL;
  1120. mutex_lock(&intel_pstate_driver_lock);
  1121. if (!intel_pstate_driver) {
  1122. mutex_unlock(&intel_pstate_driver_lock);
  1123. return -EAGAIN;
  1124. }
  1125. mutex_lock(&intel_pstate_limits_lock);
  1126. global.min_perf_pct = clamp_t(int, input,
  1127. min_perf_pct_min(), global.max_perf_pct);
  1128. mutex_unlock(&intel_pstate_limits_lock);
  1129. if (intel_pstate_driver == &intel_pstate)
  1130. intel_pstate_update_policies();
  1131. else
  1132. update_qos_request(FREQ_QOS_MIN);
  1133. mutex_unlock(&intel_pstate_driver_lock);
  1134. return count;
  1135. }
  1136. static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
  1137. struct kobj_attribute *attr, char *buf)
  1138. {
  1139. return sprintf(buf, "%u\n", hwp_boost);
  1140. }
  1141. static ssize_t store_hwp_dynamic_boost(struct kobject *a,
  1142. struct kobj_attribute *b,
  1143. const char *buf, size_t count)
  1144. {
  1145. unsigned int input;
  1146. int ret;
  1147. ret = kstrtouint(buf, 10, &input);
  1148. if (ret)
  1149. return ret;
  1150. mutex_lock(&intel_pstate_driver_lock);
  1151. hwp_boost = !!input;
  1152. intel_pstate_update_policies();
  1153. mutex_unlock(&intel_pstate_driver_lock);
  1154. return count;
  1155. }
  1156. static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
  1157. char *buf)
  1158. {
  1159. u64 power_ctl;
  1160. int enable;
  1161. rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
  1162. enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
  1163. return sprintf(buf, "%d\n", !enable);
  1164. }
  1165. static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
  1166. const char *buf, size_t count)
  1167. {
  1168. bool input;
  1169. int ret;
  1170. ret = kstrtobool(buf, &input);
  1171. if (ret)
  1172. return ret;
  1173. set_power_ctl_ee_state(input);
  1174. return count;
  1175. }
  1176. show_one(max_perf_pct, max_perf_pct);
  1177. show_one(min_perf_pct, min_perf_pct);
  1178. define_one_global_rw(status);
  1179. define_one_global_rw(no_turbo);
  1180. define_one_global_rw(max_perf_pct);
  1181. define_one_global_rw(min_perf_pct);
  1182. define_one_global_ro(turbo_pct);
  1183. define_one_global_ro(num_pstates);
  1184. define_one_global_rw(hwp_dynamic_boost);
  1185. define_one_global_rw(energy_efficiency);
  1186. static struct attribute *intel_pstate_attributes[] = {
  1187. &status.attr,
  1188. &no_turbo.attr,
  1189. NULL
  1190. };
  1191. static const struct attribute_group intel_pstate_attr_group = {
  1192. .attrs = intel_pstate_attributes,
  1193. };
  1194. static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
  1195. static struct kobject *intel_pstate_kobject;
  1196. static void __init intel_pstate_sysfs_expose_params(void)
  1197. {
  1198. int rc;
  1199. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  1200. &cpu_subsys.dev_root->kobj);
  1201. if (WARN_ON(!intel_pstate_kobject))
  1202. return;
  1203. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  1204. if (WARN_ON(rc))
  1205. return;
  1206. if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
  1207. rc = sysfs_create_file(intel_pstate_kobject, &turbo_pct.attr);
  1208. WARN_ON(rc);
  1209. rc = sysfs_create_file(intel_pstate_kobject, &num_pstates.attr);
  1210. WARN_ON(rc);
  1211. }
  1212. /*
  1213. * If per cpu limits are enforced there are no global limits, so
  1214. * return without creating max/min_perf_pct attributes
  1215. */
  1216. if (per_cpu_limits)
  1217. return;
  1218. rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
  1219. WARN_ON(rc);
  1220. rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
  1221. WARN_ON(rc);
  1222. if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
  1223. rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
  1224. WARN_ON(rc);
  1225. }
  1226. }
  1227. static void __init intel_pstate_sysfs_remove(void)
  1228. {
  1229. if (!intel_pstate_kobject)
  1230. return;
  1231. sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
  1232. if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
  1233. sysfs_remove_file(intel_pstate_kobject, &num_pstates.attr);
  1234. sysfs_remove_file(intel_pstate_kobject, &turbo_pct.attr);
  1235. }
  1236. if (!per_cpu_limits) {
  1237. sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
  1238. sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
  1239. if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
  1240. sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
  1241. }
  1242. kobject_put(intel_pstate_kobject);
  1243. }
  1244. static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
  1245. {
  1246. int rc;
  1247. if (!hwp_active)
  1248. return;
  1249. rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
  1250. WARN_ON_ONCE(rc);
  1251. }
  1252. static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
  1253. {
  1254. if (!hwp_active)
  1255. return;
  1256. sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
  1257. }
  1258. /************************** sysfs end ************************/
  1259. static void intel_pstate_notify_work(struct work_struct *work)
  1260. {
  1261. struct cpudata *cpudata =
  1262. container_of(to_delayed_work(work), struct cpudata, hwp_notify_work);
  1263. struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpudata->cpu);
  1264. if (policy) {
  1265. intel_pstate_get_hwp_cap(cpudata);
  1266. __intel_pstate_update_max_freq(cpudata, policy);
  1267. cpufreq_cpu_release(policy);
  1268. }
  1269. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
  1270. }
  1271. static DEFINE_SPINLOCK(hwp_notify_lock);
  1272. static cpumask_t hwp_intr_enable_mask;
  1273. void notify_hwp_interrupt(void)
  1274. {
  1275. unsigned int this_cpu = smp_processor_id();
  1276. struct cpudata *cpudata;
  1277. unsigned long flags;
  1278. u64 value;
  1279. if (!READ_ONCE(hwp_active) || !boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
  1280. return;
  1281. rdmsrl_safe(MSR_HWP_STATUS, &value);
  1282. if (!(value & 0x01))
  1283. return;
  1284. spin_lock_irqsave(&hwp_notify_lock, flags);
  1285. if (!cpumask_test_cpu(this_cpu, &hwp_intr_enable_mask))
  1286. goto ack_intr;
  1287. /*
  1288. * Currently we never free all_cpu_data. And we can't reach here
  1289. * without this allocated. But for safety for future changes, added
  1290. * check.
  1291. */
  1292. if (unlikely(!READ_ONCE(all_cpu_data)))
  1293. goto ack_intr;
  1294. /*
  1295. * The free is done during cleanup, when cpufreq registry is failed.
  1296. * We wouldn't be here if it fails on init or switch status. But for
  1297. * future changes, added check.
  1298. */
  1299. cpudata = READ_ONCE(all_cpu_data[this_cpu]);
  1300. if (unlikely(!cpudata))
  1301. goto ack_intr;
  1302. schedule_delayed_work(&cpudata->hwp_notify_work, msecs_to_jiffies(10));
  1303. spin_unlock_irqrestore(&hwp_notify_lock, flags);
  1304. return;
  1305. ack_intr:
  1306. wrmsrl_safe(MSR_HWP_STATUS, 0);
  1307. spin_unlock_irqrestore(&hwp_notify_lock, flags);
  1308. }
  1309. static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata)
  1310. {
  1311. unsigned long flags;
  1312. if (!boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
  1313. return;
  1314. /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */
  1315. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  1316. spin_lock_irqsave(&hwp_notify_lock, flags);
  1317. if (cpumask_test_and_clear_cpu(cpudata->cpu, &hwp_intr_enable_mask))
  1318. cancel_delayed_work(&cpudata->hwp_notify_work);
  1319. spin_unlock_irqrestore(&hwp_notify_lock, flags);
  1320. }
  1321. static void intel_pstate_enable_hwp_interrupt(struct cpudata *cpudata)
  1322. {
  1323. /* Enable HWP notification interrupt for guaranteed performance change */
  1324. if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) {
  1325. unsigned long flags;
  1326. spin_lock_irqsave(&hwp_notify_lock, flags);
  1327. INIT_DELAYED_WORK(&cpudata->hwp_notify_work, intel_pstate_notify_work);
  1328. cpumask_set_cpu(cpudata->cpu, &hwp_intr_enable_mask);
  1329. spin_unlock_irqrestore(&hwp_notify_lock, flags);
  1330. /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */
  1331. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x01);
  1332. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
  1333. }
  1334. }
  1335. static void intel_pstate_update_epp_defaults(struct cpudata *cpudata)
  1336. {
  1337. cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
  1338. /*
  1339. * If this CPU gen doesn't call for change in balance_perf
  1340. * EPP return.
  1341. */
  1342. if (epp_values[EPP_INDEX_BALANCE_PERFORMANCE] == HWP_EPP_BALANCE_PERFORMANCE)
  1343. return;
  1344. /*
  1345. * If powerup EPP is something other than chipset default 0x80 and
  1346. * - is more performance oriented than 0x80 (default balance_perf EPP)
  1347. * - But less performance oriented than performance EPP
  1348. * then use this as new balance_perf EPP.
  1349. */
  1350. if (cpudata->epp_default < HWP_EPP_BALANCE_PERFORMANCE &&
  1351. cpudata->epp_default > HWP_EPP_PERFORMANCE) {
  1352. epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = cpudata->epp_default;
  1353. return;
  1354. }
  1355. /*
  1356. * Use hard coded value per gen to update the balance_perf
  1357. * and default EPP.
  1358. */
  1359. cpudata->epp_default = epp_values[EPP_INDEX_BALANCE_PERFORMANCE];
  1360. intel_pstate_set_epp(cpudata, cpudata->epp_default);
  1361. }
  1362. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  1363. {
  1364. /* First disable HWP notification interrupt till we activate again */
  1365. if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
  1366. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  1367. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  1368. intel_pstate_enable_hwp_interrupt(cpudata);
  1369. if (cpudata->epp_default >= 0)
  1370. return;
  1371. intel_pstate_update_epp_defaults(cpudata);
  1372. }
  1373. static int atom_get_min_pstate(int not_used)
  1374. {
  1375. u64 value;
  1376. rdmsrl(MSR_ATOM_CORE_RATIOS, value);
  1377. return (value >> 8) & 0x7F;
  1378. }
  1379. static int atom_get_max_pstate(int not_used)
  1380. {
  1381. u64 value;
  1382. rdmsrl(MSR_ATOM_CORE_RATIOS, value);
  1383. return (value >> 16) & 0x7F;
  1384. }
  1385. static int atom_get_turbo_pstate(int not_used)
  1386. {
  1387. u64 value;
  1388. rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
  1389. return value & 0x7F;
  1390. }
  1391. static u64 atom_get_val(struct cpudata *cpudata, int pstate)
  1392. {
  1393. u64 val;
  1394. int32_t vid_fp;
  1395. u32 vid;
  1396. val = (u64)pstate << 8;
  1397. if (global.no_turbo && !global.turbo_disabled)
  1398. val |= (u64)1 << 32;
  1399. vid_fp = cpudata->vid.min + mul_fp(
  1400. int_tofp(pstate - cpudata->pstate.min_pstate),
  1401. cpudata->vid.ratio);
  1402. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  1403. vid = ceiling_fp(vid_fp);
  1404. if (pstate > cpudata->pstate.max_pstate)
  1405. vid = cpudata->vid.turbo;
  1406. return val | vid;
  1407. }
  1408. static int silvermont_get_scaling(void)
  1409. {
  1410. u64 value;
  1411. int i;
  1412. /* Defined in Table 35-6 from SDM (Sept 2015) */
  1413. static int silvermont_freq_table[] = {
  1414. 83300, 100000, 133300, 116700, 80000};
  1415. rdmsrl(MSR_FSB_FREQ, value);
  1416. i = value & 0x7;
  1417. WARN_ON(i > 4);
  1418. return silvermont_freq_table[i];
  1419. }
  1420. static int airmont_get_scaling(void)
  1421. {
  1422. u64 value;
  1423. int i;
  1424. /* Defined in Table 35-10 from SDM (Sept 2015) */
  1425. static int airmont_freq_table[] = {
  1426. 83300, 100000, 133300, 116700, 80000,
  1427. 93300, 90000, 88900, 87500};
  1428. rdmsrl(MSR_FSB_FREQ, value);
  1429. i = value & 0xF;
  1430. WARN_ON(i > 8);
  1431. return airmont_freq_table[i];
  1432. }
  1433. static void atom_get_vid(struct cpudata *cpudata)
  1434. {
  1435. u64 value;
  1436. rdmsrl(MSR_ATOM_CORE_VIDS, value);
  1437. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  1438. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  1439. cpudata->vid.ratio = div_fp(
  1440. cpudata->vid.max - cpudata->vid.min,
  1441. int_tofp(cpudata->pstate.max_pstate -
  1442. cpudata->pstate.min_pstate));
  1443. rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
  1444. cpudata->vid.turbo = value & 0x7f;
  1445. }
  1446. static int core_get_min_pstate(int cpu)
  1447. {
  1448. u64 value;
  1449. rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
  1450. return (value >> 40) & 0xFF;
  1451. }
  1452. static int core_get_max_pstate_physical(int cpu)
  1453. {
  1454. u64 value;
  1455. rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
  1456. return (value >> 8) & 0xFF;
  1457. }
  1458. static int core_get_tdp_ratio(int cpu, u64 plat_info)
  1459. {
  1460. /* Check how many TDP levels present */
  1461. if (plat_info & 0x600000000) {
  1462. u64 tdp_ctrl;
  1463. u64 tdp_ratio;
  1464. int tdp_msr;
  1465. int err;
  1466. /* Get the TDP level (0, 1, 2) to get ratios */
  1467. err = rdmsrl_safe_on_cpu(cpu, MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  1468. if (err)
  1469. return err;
  1470. /* TDP MSR are continuous starting at 0x648 */
  1471. tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
  1472. err = rdmsrl_safe_on_cpu(cpu, tdp_msr, &tdp_ratio);
  1473. if (err)
  1474. return err;
  1475. /* For level 1 and 2, bits[23:16] contain the ratio */
  1476. if (tdp_ctrl & 0x03)
  1477. tdp_ratio >>= 16;
  1478. tdp_ratio &= 0xff; /* ratios are only 8 bits long */
  1479. pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
  1480. return (int)tdp_ratio;
  1481. }
  1482. return -ENXIO;
  1483. }
  1484. static int core_get_max_pstate(int cpu)
  1485. {
  1486. u64 tar;
  1487. u64 plat_info;
  1488. int max_pstate;
  1489. int tdp_ratio;
  1490. int err;
  1491. rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &plat_info);
  1492. max_pstate = (plat_info >> 8) & 0xFF;
  1493. tdp_ratio = core_get_tdp_ratio(cpu, plat_info);
  1494. if (tdp_ratio <= 0)
  1495. return max_pstate;
  1496. if (hwp_active) {
  1497. /* Turbo activation ratio is not used on HWP platforms */
  1498. return tdp_ratio;
  1499. }
  1500. err = rdmsrl_safe_on_cpu(cpu, MSR_TURBO_ACTIVATION_RATIO, &tar);
  1501. if (!err) {
  1502. int tar_levels;
  1503. /* Do some sanity checking for safety */
  1504. tar_levels = tar & 0xff;
  1505. if (tdp_ratio - 1 == tar_levels) {
  1506. max_pstate = tar_levels;
  1507. pr_debug("max_pstate=TAC %x\n", max_pstate);
  1508. }
  1509. }
  1510. return max_pstate;
  1511. }
  1512. static int core_get_turbo_pstate(int cpu)
  1513. {
  1514. u64 value;
  1515. int nont, ret;
  1516. rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
  1517. nont = core_get_max_pstate(cpu);
  1518. ret = (value) & 255;
  1519. if (ret <= nont)
  1520. ret = nont;
  1521. return ret;
  1522. }
  1523. static inline int core_get_scaling(void)
  1524. {
  1525. return 100000;
  1526. }
  1527. static u64 core_get_val(struct cpudata *cpudata, int pstate)
  1528. {
  1529. u64 val;
  1530. val = (u64)pstate << 8;
  1531. if (global.no_turbo && !global.turbo_disabled)
  1532. val |= (u64)1 << 32;
  1533. return val;
  1534. }
  1535. static int knl_get_aperf_mperf_shift(void)
  1536. {
  1537. return 10;
  1538. }
  1539. static int knl_get_turbo_pstate(int cpu)
  1540. {
  1541. u64 value;
  1542. int nont, ret;
  1543. rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
  1544. nont = core_get_max_pstate(cpu);
  1545. ret = (((value) >> 8) & 0xFF);
  1546. if (ret <= nont)
  1547. ret = nont;
  1548. return ret;
  1549. }
  1550. static void hybrid_get_type(void *data)
  1551. {
  1552. u8 *cpu_type = data;
  1553. *cpu_type = get_this_hybrid_cpu_type();
  1554. }
  1555. static int hybrid_get_cpu_scaling(int cpu)
  1556. {
  1557. u8 cpu_type = 0;
  1558. smp_call_function_single(cpu, hybrid_get_type, &cpu_type, 1);
  1559. /* P-cores have a smaller perf level-to-freqency scaling factor. */
  1560. if (cpu_type == 0x40)
  1561. return 78741;
  1562. return core_get_scaling();
  1563. }
  1564. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
  1565. {
  1566. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  1567. cpu->pstate.current_pstate = pstate;
  1568. /*
  1569. * Generally, there is no guarantee that this code will always run on
  1570. * the CPU being updated, so force the register update to run on the
  1571. * right CPU.
  1572. */
  1573. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  1574. pstate_funcs.get_val(cpu, pstate));
  1575. }
  1576. static void intel_pstate_set_min_pstate(struct cpudata *cpu)
  1577. {
  1578. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
  1579. }
  1580. static void intel_pstate_max_within_limits(struct cpudata *cpu)
  1581. {
  1582. int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
  1583. update_turbo_state();
  1584. intel_pstate_set_pstate(cpu, pstate);
  1585. }
  1586. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  1587. {
  1588. int perf_ctl_max_phys = pstate_funcs.get_max_physical(cpu->cpu);
  1589. int perf_ctl_scaling = pstate_funcs.get_scaling();
  1590. cpu->pstate.min_pstate = pstate_funcs.get_min(cpu->cpu);
  1591. cpu->pstate.max_pstate_physical = perf_ctl_max_phys;
  1592. cpu->pstate.perf_ctl_scaling = perf_ctl_scaling;
  1593. if (hwp_active && !hwp_mode_bdw) {
  1594. __intel_pstate_get_hwp_cap(cpu);
  1595. if (pstate_funcs.get_cpu_scaling) {
  1596. cpu->pstate.scaling = pstate_funcs.get_cpu_scaling(cpu->cpu);
  1597. if (cpu->pstate.scaling != perf_ctl_scaling)
  1598. intel_pstate_hybrid_hwp_adjust(cpu);
  1599. } else {
  1600. cpu->pstate.scaling = perf_ctl_scaling;
  1601. }
  1602. } else {
  1603. cpu->pstate.scaling = perf_ctl_scaling;
  1604. cpu->pstate.max_pstate = pstate_funcs.get_max(cpu->cpu);
  1605. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(cpu->cpu);
  1606. }
  1607. if (cpu->pstate.scaling == perf_ctl_scaling) {
  1608. cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling;
  1609. cpu->pstate.max_freq = cpu->pstate.max_pstate * perf_ctl_scaling;
  1610. cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * perf_ctl_scaling;
  1611. }
  1612. if (pstate_funcs.get_aperf_mperf_shift)
  1613. cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
  1614. if (pstate_funcs.get_vid)
  1615. pstate_funcs.get_vid(cpu);
  1616. intel_pstate_set_min_pstate(cpu);
  1617. }
  1618. /*
  1619. * Long hold time will keep high perf limits for long time,
  1620. * which negatively impacts perf/watt for some workloads,
  1621. * like specpower. 3ms is based on experiements on some
  1622. * workoads.
  1623. */
  1624. static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
  1625. static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
  1626. {
  1627. u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
  1628. u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
  1629. u32 max_limit = (hwp_req & 0xff00) >> 8;
  1630. u32 min_limit = (hwp_req & 0xff);
  1631. u32 boost_level1;
  1632. /*
  1633. * Cases to consider (User changes via sysfs or boot time):
  1634. * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
  1635. * No boost, return.
  1636. * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
  1637. * Should result in one level boost only for P0.
  1638. * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
  1639. * Should result in two level boost:
  1640. * (min + p1)/2 and P1.
  1641. * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
  1642. * Should result in three level boost:
  1643. * (min + p1)/2, P1 and P0.
  1644. */
  1645. /* If max and min are equal or already at max, nothing to boost */
  1646. if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
  1647. return;
  1648. if (!cpu->hwp_boost_min)
  1649. cpu->hwp_boost_min = min_limit;
  1650. /* level at half way mark between min and guranteed */
  1651. boost_level1 = (HWP_GUARANTEED_PERF(hwp_cap) + min_limit) >> 1;
  1652. if (cpu->hwp_boost_min < boost_level1)
  1653. cpu->hwp_boost_min = boost_level1;
  1654. else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(hwp_cap))
  1655. cpu->hwp_boost_min = HWP_GUARANTEED_PERF(hwp_cap);
  1656. else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(hwp_cap) &&
  1657. max_limit != HWP_GUARANTEED_PERF(hwp_cap))
  1658. cpu->hwp_boost_min = max_limit;
  1659. else
  1660. return;
  1661. hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
  1662. wrmsrl(MSR_HWP_REQUEST, hwp_req);
  1663. cpu->last_update = cpu->sample.time;
  1664. }
  1665. static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
  1666. {
  1667. if (cpu->hwp_boost_min) {
  1668. bool expired;
  1669. /* Check if we are idle for hold time to boost down */
  1670. expired = time_after64(cpu->sample.time, cpu->last_update +
  1671. hwp_boost_hold_time_ns);
  1672. if (expired) {
  1673. wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
  1674. cpu->hwp_boost_min = 0;
  1675. }
  1676. }
  1677. cpu->last_update = cpu->sample.time;
  1678. }
  1679. static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
  1680. u64 time)
  1681. {
  1682. cpu->sample.time = time;
  1683. if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
  1684. bool do_io = false;
  1685. cpu->sched_flags = 0;
  1686. /*
  1687. * Set iowait_boost flag and update time. Since IO WAIT flag
  1688. * is set all the time, we can't just conclude that there is
  1689. * some IO bound activity is scheduled on this CPU with just
  1690. * one occurrence. If we receive at least two in two
  1691. * consecutive ticks, then we treat as boost candidate.
  1692. */
  1693. if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
  1694. do_io = true;
  1695. cpu->last_io_update = time;
  1696. if (do_io)
  1697. intel_pstate_hwp_boost_up(cpu);
  1698. } else {
  1699. intel_pstate_hwp_boost_down(cpu);
  1700. }
  1701. }
  1702. static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
  1703. u64 time, unsigned int flags)
  1704. {
  1705. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1706. cpu->sched_flags |= flags;
  1707. if (smp_processor_id() == cpu->cpu)
  1708. intel_pstate_update_util_hwp_local(cpu, time);
  1709. }
  1710. static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
  1711. {
  1712. struct sample *sample = &cpu->sample;
  1713. sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
  1714. }
  1715. static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
  1716. {
  1717. u64 aperf, mperf;
  1718. unsigned long flags;
  1719. u64 tsc;
  1720. local_irq_save(flags);
  1721. rdmsrl(MSR_IA32_APERF, aperf);
  1722. rdmsrl(MSR_IA32_MPERF, mperf);
  1723. tsc = rdtsc();
  1724. if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
  1725. local_irq_restore(flags);
  1726. return false;
  1727. }
  1728. local_irq_restore(flags);
  1729. cpu->last_sample_time = cpu->sample.time;
  1730. cpu->sample.time = time;
  1731. cpu->sample.aperf = aperf;
  1732. cpu->sample.mperf = mperf;
  1733. cpu->sample.tsc = tsc;
  1734. cpu->sample.aperf -= cpu->prev_aperf;
  1735. cpu->sample.mperf -= cpu->prev_mperf;
  1736. cpu->sample.tsc -= cpu->prev_tsc;
  1737. cpu->prev_aperf = aperf;
  1738. cpu->prev_mperf = mperf;
  1739. cpu->prev_tsc = tsc;
  1740. /*
  1741. * First time this function is invoked in a given cycle, all of the
  1742. * previous sample data fields are equal to zero or stale and they must
  1743. * be populated with meaningful numbers for things to work, so assume
  1744. * that sample.time will always be reset before setting the utilization
  1745. * update hook and make the caller skip the sample then.
  1746. */
  1747. if (cpu->last_sample_time) {
  1748. intel_pstate_calc_avg_perf(cpu);
  1749. return true;
  1750. }
  1751. return false;
  1752. }
  1753. static inline int32_t get_avg_frequency(struct cpudata *cpu)
  1754. {
  1755. return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
  1756. }
  1757. static inline int32_t get_avg_pstate(struct cpudata *cpu)
  1758. {
  1759. return mul_ext_fp(cpu->pstate.max_pstate_physical,
  1760. cpu->sample.core_avg_perf);
  1761. }
  1762. static inline int32_t get_target_pstate(struct cpudata *cpu)
  1763. {
  1764. struct sample *sample = &cpu->sample;
  1765. int32_t busy_frac;
  1766. int target, avg_pstate;
  1767. busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
  1768. sample->tsc);
  1769. if (busy_frac < cpu->iowait_boost)
  1770. busy_frac = cpu->iowait_boost;
  1771. sample->busy_scaled = busy_frac * 100;
  1772. target = global.no_turbo || global.turbo_disabled ?
  1773. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1774. target += target >> 2;
  1775. target = mul_fp(target, busy_frac);
  1776. if (target < cpu->pstate.min_pstate)
  1777. target = cpu->pstate.min_pstate;
  1778. /*
  1779. * If the average P-state during the previous cycle was higher than the
  1780. * current target, add 50% of the difference to the target to reduce
  1781. * possible performance oscillations and offset possible performance
  1782. * loss related to moving the workload from one CPU to another within
  1783. * a package/module.
  1784. */
  1785. avg_pstate = get_avg_pstate(cpu);
  1786. if (avg_pstate > target)
  1787. target += (avg_pstate - target) >> 1;
  1788. return target;
  1789. }
  1790. static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
  1791. {
  1792. int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
  1793. int max_pstate = max(min_pstate, cpu->max_perf_ratio);
  1794. return clamp_t(int, pstate, min_pstate, max_pstate);
  1795. }
  1796. static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
  1797. {
  1798. if (pstate == cpu->pstate.current_pstate)
  1799. return;
  1800. cpu->pstate.current_pstate = pstate;
  1801. wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
  1802. }
  1803. static void intel_pstate_adjust_pstate(struct cpudata *cpu)
  1804. {
  1805. int from = cpu->pstate.current_pstate;
  1806. struct sample *sample;
  1807. int target_pstate;
  1808. update_turbo_state();
  1809. target_pstate = get_target_pstate(cpu);
  1810. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  1811. trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
  1812. intel_pstate_update_pstate(cpu, target_pstate);
  1813. sample = &cpu->sample;
  1814. trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
  1815. fp_toint(sample->busy_scaled),
  1816. from,
  1817. cpu->pstate.current_pstate,
  1818. sample->mperf,
  1819. sample->aperf,
  1820. sample->tsc,
  1821. get_avg_frequency(cpu),
  1822. fp_toint(cpu->iowait_boost * 100));
  1823. }
  1824. static void intel_pstate_update_util(struct update_util_data *data, u64 time,
  1825. unsigned int flags)
  1826. {
  1827. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1828. u64 delta_ns;
  1829. /* Don't allow remote callbacks */
  1830. if (smp_processor_id() != cpu->cpu)
  1831. return;
  1832. delta_ns = time - cpu->last_update;
  1833. if (flags & SCHED_CPUFREQ_IOWAIT) {
  1834. /* Start over if the CPU may have been idle. */
  1835. if (delta_ns > TICK_NSEC) {
  1836. cpu->iowait_boost = ONE_EIGHTH_FP;
  1837. } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
  1838. cpu->iowait_boost <<= 1;
  1839. if (cpu->iowait_boost > int_tofp(1))
  1840. cpu->iowait_boost = int_tofp(1);
  1841. } else {
  1842. cpu->iowait_boost = ONE_EIGHTH_FP;
  1843. }
  1844. } else if (cpu->iowait_boost) {
  1845. /* Clear iowait_boost if the CPU may have been idle. */
  1846. if (delta_ns > TICK_NSEC)
  1847. cpu->iowait_boost = 0;
  1848. else
  1849. cpu->iowait_boost >>= 1;
  1850. }
  1851. cpu->last_update = time;
  1852. delta_ns = time - cpu->sample.time;
  1853. if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
  1854. return;
  1855. if (intel_pstate_sample(cpu, time))
  1856. intel_pstate_adjust_pstate(cpu);
  1857. }
  1858. static struct pstate_funcs core_funcs = {
  1859. .get_max = core_get_max_pstate,
  1860. .get_max_physical = core_get_max_pstate_physical,
  1861. .get_min = core_get_min_pstate,
  1862. .get_turbo = core_get_turbo_pstate,
  1863. .get_scaling = core_get_scaling,
  1864. .get_val = core_get_val,
  1865. };
  1866. static const struct pstate_funcs silvermont_funcs = {
  1867. .get_max = atom_get_max_pstate,
  1868. .get_max_physical = atom_get_max_pstate,
  1869. .get_min = atom_get_min_pstate,
  1870. .get_turbo = atom_get_turbo_pstate,
  1871. .get_val = atom_get_val,
  1872. .get_scaling = silvermont_get_scaling,
  1873. .get_vid = atom_get_vid,
  1874. };
  1875. static const struct pstate_funcs airmont_funcs = {
  1876. .get_max = atom_get_max_pstate,
  1877. .get_max_physical = atom_get_max_pstate,
  1878. .get_min = atom_get_min_pstate,
  1879. .get_turbo = atom_get_turbo_pstate,
  1880. .get_val = atom_get_val,
  1881. .get_scaling = airmont_get_scaling,
  1882. .get_vid = atom_get_vid,
  1883. };
  1884. static const struct pstate_funcs knl_funcs = {
  1885. .get_max = core_get_max_pstate,
  1886. .get_max_physical = core_get_max_pstate_physical,
  1887. .get_min = core_get_min_pstate,
  1888. .get_turbo = knl_get_turbo_pstate,
  1889. .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
  1890. .get_scaling = core_get_scaling,
  1891. .get_val = core_get_val,
  1892. };
  1893. #define X86_MATCH(model, policy) \
  1894. X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
  1895. X86_FEATURE_APERFMPERF, &policy)
  1896. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  1897. X86_MATCH(SANDYBRIDGE, core_funcs),
  1898. X86_MATCH(SANDYBRIDGE_X, core_funcs),
  1899. X86_MATCH(ATOM_SILVERMONT, silvermont_funcs),
  1900. X86_MATCH(IVYBRIDGE, core_funcs),
  1901. X86_MATCH(HASWELL, core_funcs),
  1902. X86_MATCH(BROADWELL, core_funcs),
  1903. X86_MATCH(IVYBRIDGE_X, core_funcs),
  1904. X86_MATCH(HASWELL_X, core_funcs),
  1905. X86_MATCH(HASWELL_L, core_funcs),
  1906. X86_MATCH(HASWELL_G, core_funcs),
  1907. X86_MATCH(BROADWELL_G, core_funcs),
  1908. X86_MATCH(ATOM_AIRMONT, airmont_funcs),
  1909. X86_MATCH(SKYLAKE_L, core_funcs),
  1910. X86_MATCH(BROADWELL_X, core_funcs),
  1911. X86_MATCH(SKYLAKE, core_funcs),
  1912. X86_MATCH(BROADWELL_D, core_funcs),
  1913. X86_MATCH(XEON_PHI_KNL, knl_funcs),
  1914. X86_MATCH(XEON_PHI_KNM, knl_funcs),
  1915. X86_MATCH(ATOM_GOLDMONT, core_funcs),
  1916. X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs),
  1917. X86_MATCH(SKYLAKE_X, core_funcs),
  1918. X86_MATCH(COMETLAKE, core_funcs),
  1919. X86_MATCH(ICELAKE_X, core_funcs),
  1920. X86_MATCH(TIGERLAKE, core_funcs),
  1921. {}
  1922. };
  1923. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  1924. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
  1925. X86_MATCH(BROADWELL_D, core_funcs),
  1926. X86_MATCH(BROADWELL_X, core_funcs),
  1927. X86_MATCH(SKYLAKE_X, core_funcs),
  1928. X86_MATCH(ICELAKE_X, core_funcs),
  1929. X86_MATCH(SAPPHIRERAPIDS_X, core_funcs),
  1930. {}
  1931. };
  1932. static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
  1933. X86_MATCH(KABYLAKE, core_funcs),
  1934. {}
  1935. };
  1936. static const struct x86_cpu_id intel_pstate_hwp_boost_ids[] = {
  1937. X86_MATCH(SKYLAKE_X, core_funcs),
  1938. X86_MATCH(SKYLAKE, core_funcs),
  1939. {}
  1940. };
  1941. static int intel_pstate_init_cpu(unsigned int cpunum)
  1942. {
  1943. struct cpudata *cpu;
  1944. cpu = all_cpu_data[cpunum];
  1945. if (!cpu) {
  1946. cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
  1947. if (!cpu)
  1948. return -ENOMEM;
  1949. WRITE_ONCE(all_cpu_data[cpunum], cpu);
  1950. cpu->cpu = cpunum;
  1951. cpu->epp_default = -EINVAL;
  1952. if (hwp_active) {
  1953. const struct x86_cpu_id *id;
  1954. intel_pstate_hwp_enable(cpu);
  1955. id = x86_match_cpu(intel_pstate_hwp_boost_ids);
  1956. if (id && intel_pstate_acpi_pm_profile_server())
  1957. hwp_boost = true;
  1958. }
  1959. } else if (hwp_active) {
  1960. /*
  1961. * Re-enable HWP in case this happens after a resume from ACPI
  1962. * S3 if the CPU was offline during the whole system/resume
  1963. * cycle.
  1964. */
  1965. intel_pstate_hwp_reenable(cpu);
  1966. }
  1967. cpu->epp_powersave = -EINVAL;
  1968. cpu->epp_policy = 0;
  1969. intel_pstate_get_cpu_pstates(cpu);
  1970. pr_debug("controlling: cpu %d\n", cpunum);
  1971. return 0;
  1972. }
  1973. static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
  1974. {
  1975. struct cpudata *cpu = all_cpu_data[cpu_num];
  1976. if (hwp_active && !hwp_boost)
  1977. return;
  1978. if (cpu->update_util_set)
  1979. return;
  1980. /* Prevent intel_pstate_update_util() from using stale data. */
  1981. cpu->sample.time = 0;
  1982. cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
  1983. (hwp_active ?
  1984. intel_pstate_update_util_hwp :
  1985. intel_pstate_update_util));
  1986. cpu->update_util_set = true;
  1987. }
  1988. static void intel_pstate_clear_update_util_hook(unsigned int cpu)
  1989. {
  1990. struct cpudata *cpu_data = all_cpu_data[cpu];
  1991. if (!cpu_data->update_util_set)
  1992. return;
  1993. cpufreq_remove_update_util_hook(cpu);
  1994. cpu_data->update_util_set = false;
  1995. synchronize_rcu();
  1996. }
  1997. static int intel_pstate_get_max_freq(struct cpudata *cpu)
  1998. {
  1999. return global.turbo_disabled || global.no_turbo ?
  2000. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  2001. }
  2002. static void intel_pstate_update_perf_limits(struct cpudata *cpu,
  2003. unsigned int policy_min,
  2004. unsigned int policy_max)
  2005. {
  2006. int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
  2007. int32_t max_policy_perf, min_policy_perf;
  2008. max_policy_perf = policy_max / perf_ctl_scaling;
  2009. if (policy_max == policy_min) {
  2010. min_policy_perf = max_policy_perf;
  2011. } else {
  2012. min_policy_perf = policy_min / perf_ctl_scaling;
  2013. min_policy_perf = clamp_t(int32_t, min_policy_perf,
  2014. 0, max_policy_perf);
  2015. }
  2016. /*
  2017. * HWP needs some special consideration, because HWP_REQUEST uses
  2018. * abstract values to represent performance rather than pure ratios.
  2019. */
  2020. if (hwp_active && cpu->pstate.scaling != perf_ctl_scaling) {
  2021. int scaling = cpu->pstate.scaling;
  2022. int freq;
  2023. freq = max_policy_perf * perf_ctl_scaling;
  2024. max_policy_perf = DIV_ROUND_UP(freq, scaling);
  2025. freq = min_policy_perf * perf_ctl_scaling;
  2026. min_policy_perf = DIV_ROUND_UP(freq, scaling);
  2027. }
  2028. pr_debug("cpu:%d min_policy_perf:%d max_policy_perf:%d\n",
  2029. cpu->cpu, min_policy_perf, max_policy_perf);
  2030. /* Normalize user input to [min_perf, max_perf] */
  2031. if (per_cpu_limits) {
  2032. cpu->min_perf_ratio = min_policy_perf;
  2033. cpu->max_perf_ratio = max_policy_perf;
  2034. } else {
  2035. int turbo_max = cpu->pstate.turbo_pstate;
  2036. int32_t global_min, global_max;
  2037. /* Global limits are in percent of the maximum turbo P-state. */
  2038. global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
  2039. global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
  2040. global_min = clamp_t(int32_t, global_min, 0, global_max);
  2041. pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
  2042. global_min, global_max);
  2043. cpu->min_perf_ratio = max(min_policy_perf, global_min);
  2044. cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
  2045. cpu->max_perf_ratio = min(max_policy_perf, global_max);
  2046. cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
  2047. /* Make sure min_perf <= max_perf */
  2048. cpu->min_perf_ratio = min(cpu->min_perf_ratio,
  2049. cpu->max_perf_ratio);
  2050. }
  2051. pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
  2052. cpu->max_perf_ratio,
  2053. cpu->min_perf_ratio);
  2054. }
  2055. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  2056. {
  2057. struct cpudata *cpu;
  2058. if (!policy->cpuinfo.max_freq)
  2059. return -ENODEV;
  2060. pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
  2061. policy->cpuinfo.max_freq, policy->max);
  2062. cpu = all_cpu_data[policy->cpu];
  2063. cpu->policy = policy->policy;
  2064. mutex_lock(&intel_pstate_limits_lock);
  2065. intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
  2066. if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
  2067. /*
  2068. * NOHZ_FULL CPUs need this as the governor callback may not
  2069. * be invoked on them.
  2070. */
  2071. intel_pstate_clear_update_util_hook(policy->cpu);
  2072. intel_pstate_max_within_limits(cpu);
  2073. } else {
  2074. intel_pstate_set_update_util_hook(policy->cpu);
  2075. }
  2076. if (hwp_active) {
  2077. /*
  2078. * When hwp_boost was active before and dynamically it
  2079. * was turned off, in that case we need to clear the
  2080. * update util hook.
  2081. */
  2082. if (!hwp_boost)
  2083. intel_pstate_clear_update_util_hook(policy->cpu);
  2084. intel_pstate_hwp_set(policy->cpu);
  2085. }
  2086. /*
  2087. * policy->cur is never updated with the intel_pstate driver, but it
  2088. * is used as a stale frequency value. So, keep it within limits.
  2089. */
  2090. policy->cur = policy->min;
  2091. mutex_unlock(&intel_pstate_limits_lock);
  2092. return 0;
  2093. }
  2094. static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
  2095. struct cpufreq_policy_data *policy)
  2096. {
  2097. if (!hwp_active &&
  2098. cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
  2099. policy->max < policy->cpuinfo.max_freq &&
  2100. policy->max > cpu->pstate.max_freq) {
  2101. pr_debug("policy->max > max non turbo frequency\n");
  2102. policy->max = policy->cpuinfo.max_freq;
  2103. }
  2104. }
  2105. static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
  2106. struct cpufreq_policy_data *policy)
  2107. {
  2108. int max_freq;
  2109. update_turbo_state();
  2110. if (hwp_active) {
  2111. intel_pstate_get_hwp_cap(cpu);
  2112. max_freq = global.no_turbo || global.turbo_disabled ?
  2113. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  2114. } else {
  2115. max_freq = intel_pstate_get_max_freq(cpu);
  2116. }
  2117. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
  2118. intel_pstate_adjust_policy_max(cpu, policy);
  2119. }
  2120. static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
  2121. {
  2122. intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
  2123. return 0;
  2124. }
  2125. static int intel_cpufreq_cpu_offline(struct cpufreq_policy *policy)
  2126. {
  2127. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2128. pr_debug("CPU %d going offline\n", cpu->cpu);
  2129. if (cpu->suspended)
  2130. return 0;
  2131. /*
  2132. * If the CPU is an SMT thread and it goes offline with the performance
  2133. * settings different from the minimum, it will prevent its sibling
  2134. * from getting to lower performance levels, so force the minimum
  2135. * performance on CPU offline to prevent that from happening.
  2136. */
  2137. if (hwp_active)
  2138. intel_pstate_hwp_offline(cpu);
  2139. else
  2140. intel_pstate_set_min_pstate(cpu);
  2141. intel_pstate_exit_perf_limits(policy);
  2142. return 0;
  2143. }
  2144. static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
  2145. {
  2146. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2147. pr_debug("CPU %d going online\n", cpu->cpu);
  2148. intel_pstate_init_acpi_perf_limits(policy);
  2149. if (hwp_active) {
  2150. /*
  2151. * Re-enable HWP and clear the "suspended" flag to let "resume"
  2152. * know that it need not do that.
  2153. */
  2154. intel_pstate_hwp_reenable(cpu);
  2155. cpu->suspended = false;
  2156. }
  2157. return 0;
  2158. }
  2159. static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
  2160. {
  2161. intel_pstate_clear_update_util_hook(policy->cpu);
  2162. return intel_cpufreq_cpu_offline(policy);
  2163. }
  2164. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  2165. {
  2166. pr_debug("CPU %d exiting\n", policy->cpu);
  2167. policy->fast_switch_possible = false;
  2168. return 0;
  2169. }
  2170. static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
  2171. {
  2172. struct cpudata *cpu;
  2173. int rc;
  2174. rc = intel_pstate_init_cpu(policy->cpu);
  2175. if (rc)
  2176. return rc;
  2177. cpu = all_cpu_data[policy->cpu];
  2178. cpu->max_perf_ratio = 0xFF;
  2179. cpu->min_perf_ratio = 0;
  2180. /* cpuinfo and default policy values */
  2181. policy->cpuinfo.min_freq = cpu->pstate.min_freq;
  2182. update_turbo_state();
  2183. global.turbo_disabled_mf = global.turbo_disabled;
  2184. policy->cpuinfo.max_freq = global.turbo_disabled ?
  2185. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  2186. policy->min = policy->cpuinfo.min_freq;
  2187. policy->max = policy->cpuinfo.max_freq;
  2188. intel_pstate_init_acpi_perf_limits(policy);
  2189. policy->fast_switch_possible = true;
  2190. return 0;
  2191. }
  2192. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  2193. {
  2194. int ret = __intel_pstate_cpu_init(policy);
  2195. if (ret)
  2196. return ret;
  2197. /*
  2198. * Set the policy to powersave to provide a valid fallback value in case
  2199. * the default cpufreq governor is neither powersave nor performance.
  2200. */
  2201. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  2202. if (hwp_active) {
  2203. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2204. cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
  2205. }
  2206. return 0;
  2207. }
  2208. static struct cpufreq_driver intel_pstate = {
  2209. .flags = CPUFREQ_CONST_LOOPS,
  2210. .verify = intel_pstate_verify_policy,
  2211. .setpolicy = intel_pstate_set_policy,
  2212. .suspend = intel_pstate_suspend,
  2213. .resume = intel_pstate_resume,
  2214. .init = intel_pstate_cpu_init,
  2215. .exit = intel_pstate_cpu_exit,
  2216. .offline = intel_pstate_cpu_offline,
  2217. .online = intel_pstate_cpu_online,
  2218. .update_limits = intel_pstate_update_limits,
  2219. .name = "intel_pstate",
  2220. };
  2221. static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
  2222. {
  2223. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2224. intel_pstate_verify_cpu_policy(cpu, policy);
  2225. intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
  2226. return 0;
  2227. }
  2228. /* Use of trace in passive mode:
  2229. *
  2230. * In passive mode the trace core_busy field (also known as the
  2231. * performance field, and lablelled as such on the graphs; also known as
  2232. * core_avg_perf) is not needed and so is re-assigned to indicate if the
  2233. * driver call was via the normal or fast switch path. Various graphs
  2234. * output from the intel_pstate_tracer.py utility that include core_busy
  2235. * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
  2236. * so we use 10 to indicate the normal path through the driver, and
  2237. * 90 to indicate the fast switch path through the driver.
  2238. * The scaled_busy field is not used, and is set to 0.
  2239. */
  2240. #define INTEL_PSTATE_TRACE_TARGET 10
  2241. #define INTEL_PSTATE_TRACE_FAST_SWITCH 90
  2242. static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
  2243. {
  2244. struct sample *sample;
  2245. if (!trace_pstate_sample_enabled())
  2246. return;
  2247. if (!intel_pstate_sample(cpu, ktime_get()))
  2248. return;
  2249. sample = &cpu->sample;
  2250. trace_pstate_sample(trace_type,
  2251. 0,
  2252. old_pstate,
  2253. cpu->pstate.current_pstate,
  2254. sample->mperf,
  2255. sample->aperf,
  2256. sample->tsc,
  2257. get_avg_frequency(cpu),
  2258. fp_toint(cpu->iowait_boost * 100));
  2259. }
  2260. static void intel_cpufreq_hwp_update(struct cpudata *cpu, u32 min, u32 max,
  2261. u32 desired, bool fast_switch)
  2262. {
  2263. u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
  2264. value &= ~HWP_MIN_PERF(~0L);
  2265. value |= HWP_MIN_PERF(min);
  2266. value &= ~HWP_MAX_PERF(~0L);
  2267. value |= HWP_MAX_PERF(max);
  2268. value &= ~HWP_DESIRED_PERF(~0L);
  2269. value |= HWP_DESIRED_PERF(desired);
  2270. if (value == prev)
  2271. return;
  2272. WRITE_ONCE(cpu->hwp_req_cached, value);
  2273. if (fast_switch)
  2274. wrmsrl(MSR_HWP_REQUEST, value);
  2275. else
  2276. wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
  2277. }
  2278. static void intel_cpufreq_perf_ctl_update(struct cpudata *cpu,
  2279. u32 target_pstate, bool fast_switch)
  2280. {
  2281. if (fast_switch)
  2282. wrmsrl(MSR_IA32_PERF_CTL,
  2283. pstate_funcs.get_val(cpu, target_pstate));
  2284. else
  2285. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  2286. pstate_funcs.get_val(cpu, target_pstate));
  2287. }
  2288. static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
  2289. int target_pstate, bool fast_switch)
  2290. {
  2291. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2292. int old_pstate = cpu->pstate.current_pstate;
  2293. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  2294. if (hwp_active) {
  2295. int max_pstate = policy->strict_target ?
  2296. target_pstate : cpu->max_perf_ratio;
  2297. intel_cpufreq_hwp_update(cpu, target_pstate, max_pstate, 0,
  2298. fast_switch);
  2299. } else if (target_pstate != old_pstate) {
  2300. intel_cpufreq_perf_ctl_update(cpu, target_pstate, fast_switch);
  2301. }
  2302. cpu->pstate.current_pstate = target_pstate;
  2303. intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
  2304. INTEL_PSTATE_TRACE_TARGET, old_pstate);
  2305. return target_pstate;
  2306. }
  2307. static int intel_cpufreq_target(struct cpufreq_policy *policy,
  2308. unsigned int target_freq,
  2309. unsigned int relation)
  2310. {
  2311. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2312. struct cpufreq_freqs freqs;
  2313. int target_pstate;
  2314. update_turbo_state();
  2315. freqs.old = policy->cur;
  2316. freqs.new = target_freq;
  2317. cpufreq_freq_transition_begin(policy, &freqs);
  2318. switch (relation) {
  2319. case CPUFREQ_RELATION_L:
  2320. target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
  2321. break;
  2322. case CPUFREQ_RELATION_H:
  2323. target_pstate = freqs.new / cpu->pstate.scaling;
  2324. break;
  2325. default:
  2326. target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
  2327. break;
  2328. }
  2329. target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
  2330. freqs.new = target_pstate * cpu->pstate.scaling;
  2331. cpufreq_freq_transition_end(policy, &freqs, false);
  2332. return 0;
  2333. }
  2334. static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
  2335. unsigned int target_freq)
  2336. {
  2337. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2338. int target_pstate;
  2339. update_turbo_state();
  2340. target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
  2341. target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
  2342. return target_pstate * cpu->pstate.scaling;
  2343. }
  2344. static void intel_cpufreq_adjust_perf(unsigned int cpunum,
  2345. unsigned long min_perf,
  2346. unsigned long target_perf,
  2347. unsigned long capacity)
  2348. {
  2349. struct cpudata *cpu = all_cpu_data[cpunum];
  2350. u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
  2351. int old_pstate = cpu->pstate.current_pstate;
  2352. int cap_pstate, min_pstate, max_pstate, target_pstate;
  2353. update_turbo_state();
  2354. cap_pstate = global.turbo_disabled ? HWP_GUARANTEED_PERF(hwp_cap) :
  2355. HWP_HIGHEST_PERF(hwp_cap);
  2356. /* Optimization: Avoid unnecessary divisions. */
  2357. target_pstate = cap_pstate;
  2358. if (target_perf < capacity)
  2359. target_pstate = DIV_ROUND_UP(cap_pstate * target_perf, capacity);
  2360. min_pstate = cap_pstate;
  2361. if (min_perf < capacity)
  2362. min_pstate = DIV_ROUND_UP(cap_pstate * min_perf, capacity);
  2363. if (min_pstate < cpu->pstate.min_pstate)
  2364. min_pstate = cpu->pstate.min_pstate;
  2365. if (min_pstate < cpu->min_perf_ratio)
  2366. min_pstate = cpu->min_perf_ratio;
  2367. max_pstate = min(cap_pstate, cpu->max_perf_ratio);
  2368. if (max_pstate < min_pstate)
  2369. max_pstate = min_pstate;
  2370. target_pstate = clamp_t(int, target_pstate, min_pstate, max_pstate);
  2371. intel_cpufreq_hwp_update(cpu, min_pstate, max_pstate, target_pstate, true);
  2372. cpu->pstate.current_pstate = target_pstate;
  2373. intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
  2374. }
  2375. static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
  2376. {
  2377. struct freq_qos_request *req;
  2378. struct cpudata *cpu;
  2379. struct device *dev;
  2380. int ret, freq;
  2381. dev = get_cpu_device(policy->cpu);
  2382. if (!dev)
  2383. return -ENODEV;
  2384. ret = __intel_pstate_cpu_init(policy);
  2385. if (ret)
  2386. return ret;
  2387. policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
  2388. /* This reflects the intel_pstate_get_cpu_pstates() setting. */
  2389. policy->cur = policy->cpuinfo.min_freq;
  2390. req = kcalloc(2, sizeof(*req), GFP_KERNEL);
  2391. if (!req) {
  2392. ret = -ENOMEM;
  2393. goto pstate_exit;
  2394. }
  2395. cpu = all_cpu_data[policy->cpu];
  2396. if (hwp_active) {
  2397. u64 value;
  2398. policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
  2399. intel_pstate_get_hwp_cap(cpu);
  2400. rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
  2401. WRITE_ONCE(cpu->hwp_req_cached, value);
  2402. cpu->epp_cached = intel_pstate_get_epp(cpu, value);
  2403. } else {
  2404. policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
  2405. }
  2406. freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.min_perf_pct, 100);
  2407. ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
  2408. freq);
  2409. if (ret < 0) {
  2410. dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
  2411. goto free_req;
  2412. }
  2413. freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.max_perf_pct, 100);
  2414. ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
  2415. freq);
  2416. if (ret < 0) {
  2417. dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
  2418. goto remove_min_req;
  2419. }
  2420. policy->driver_data = req;
  2421. return 0;
  2422. remove_min_req:
  2423. freq_qos_remove_request(req);
  2424. free_req:
  2425. kfree(req);
  2426. pstate_exit:
  2427. intel_pstate_exit_perf_limits(policy);
  2428. return ret;
  2429. }
  2430. static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  2431. {
  2432. struct freq_qos_request *req;
  2433. req = policy->driver_data;
  2434. freq_qos_remove_request(req + 1);
  2435. freq_qos_remove_request(req);
  2436. kfree(req);
  2437. return intel_pstate_cpu_exit(policy);
  2438. }
  2439. static int intel_cpufreq_suspend(struct cpufreq_policy *policy)
  2440. {
  2441. intel_pstate_suspend(policy);
  2442. if (hwp_active) {
  2443. struct cpudata *cpu = all_cpu_data[policy->cpu];
  2444. u64 value = READ_ONCE(cpu->hwp_req_cached);
  2445. /*
  2446. * Clear the desired perf field in MSR_HWP_REQUEST in case
  2447. * intel_cpufreq_adjust_perf() is in use and the last value
  2448. * written by it may not be suitable.
  2449. */
  2450. value &= ~HWP_DESIRED_PERF(~0L);
  2451. wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
  2452. WRITE_ONCE(cpu->hwp_req_cached, value);
  2453. }
  2454. return 0;
  2455. }
  2456. static struct cpufreq_driver intel_cpufreq = {
  2457. .flags = CPUFREQ_CONST_LOOPS,
  2458. .verify = intel_cpufreq_verify_policy,
  2459. .target = intel_cpufreq_target,
  2460. .fast_switch = intel_cpufreq_fast_switch,
  2461. .init = intel_cpufreq_cpu_init,
  2462. .exit = intel_cpufreq_cpu_exit,
  2463. .offline = intel_cpufreq_cpu_offline,
  2464. .online = intel_pstate_cpu_online,
  2465. .suspend = intel_cpufreq_suspend,
  2466. .resume = intel_pstate_resume,
  2467. .update_limits = intel_pstate_update_limits,
  2468. .name = "intel_cpufreq",
  2469. };
  2470. static struct cpufreq_driver *default_driver;
  2471. static void intel_pstate_driver_cleanup(void)
  2472. {
  2473. unsigned int cpu;
  2474. cpus_read_lock();
  2475. for_each_online_cpu(cpu) {
  2476. if (all_cpu_data[cpu]) {
  2477. if (intel_pstate_driver == &intel_pstate)
  2478. intel_pstate_clear_update_util_hook(cpu);
  2479. spin_lock(&hwp_notify_lock);
  2480. kfree(all_cpu_data[cpu]);
  2481. WRITE_ONCE(all_cpu_data[cpu], NULL);
  2482. spin_unlock(&hwp_notify_lock);
  2483. }
  2484. }
  2485. cpus_read_unlock();
  2486. intel_pstate_driver = NULL;
  2487. }
  2488. static int intel_pstate_register_driver(struct cpufreq_driver *driver)
  2489. {
  2490. int ret;
  2491. if (driver == &intel_pstate)
  2492. intel_pstate_sysfs_expose_hwp_dynamic_boost();
  2493. memset(&global, 0, sizeof(global));
  2494. global.max_perf_pct = 100;
  2495. intel_pstate_driver = driver;
  2496. ret = cpufreq_register_driver(intel_pstate_driver);
  2497. if (ret) {
  2498. intel_pstate_driver_cleanup();
  2499. return ret;
  2500. }
  2501. global.min_perf_pct = min_perf_pct_min();
  2502. return 0;
  2503. }
  2504. static ssize_t intel_pstate_show_status(char *buf)
  2505. {
  2506. if (!intel_pstate_driver)
  2507. return sprintf(buf, "off\n");
  2508. return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
  2509. "active" : "passive");
  2510. }
  2511. static int intel_pstate_update_status(const char *buf, size_t size)
  2512. {
  2513. if (size == 3 && !strncmp(buf, "off", size)) {
  2514. if (!intel_pstate_driver)
  2515. return -EINVAL;
  2516. if (hwp_active)
  2517. return -EBUSY;
  2518. cpufreq_unregister_driver(intel_pstate_driver);
  2519. intel_pstate_driver_cleanup();
  2520. return 0;
  2521. }
  2522. if (size == 6 && !strncmp(buf, "active", size)) {
  2523. if (intel_pstate_driver) {
  2524. if (intel_pstate_driver == &intel_pstate)
  2525. return 0;
  2526. cpufreq_unregister_driver(intel_pstate_driver);
  2527. }
  2528. return intel_pstate_register_driver(&intel_pstate);
  2529. }
  2530. if (size == 7 && !strncmp(buf, "passive", size)) {
  2531. if (intel_pstate_driver) {
  2532. if (intel_pstate_driver == &intel_cpufreq)
  2533. return 0;
  2534. cpufreq_unregister_driver(intel_pstate_driver);
  2535. intel_pstate_sysfs_hide_hwp_dynamic_boost();
  2536. }
  2537. return intel_pstate_register_driver(&intel_cpufreq);
  2538. }
  2539. return -EINVAL;
  2540. }
  2541. static int no_load __initdata;
  2542. static int no_hwp __initdata;
  2543. static int hwp_only __initdata;
  2544. static unsigned int force_load __initdata;
  2545. static int __init intel_pstate_msrs_not_valid(void)
  2546. {
  2547. if (!pstate_funcs.get_max(0) ||
  2548. !pstate_funcs.get_min(0) ||
  2549. !pstate_funcs.get_turbo(0))
  2550. return -ENODEV;
  2551. return 0;
  2552. }
  2553. static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
  2554. {
  2555. pstate_funcs.get_max = funcs->get_max;
  2556. pstate_funcs.get_max_physical = funcs->get_max_physical;
  2557. pstate_funcs.get_min = funcs->get_min;
  2558. pstate_funcs.get_turbo = funcs->get_turbo;
  2559. pstate_funcs.get_scaling = funcs->get_scaling;
  2560. pstate_funcs.get_val = funcs->get_val;
  2561. pstate_funcs.get_vid = funcs->get_vid;
  2562. pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
  2563. }
  2564. #ifdef CONFIG_ACPI
  2565. static bool __init intel_pstate_no_acpi_pss(void)
  2566. {
  2567. int i;
  2568. for_each_possible_cpu(i) {
  2569. acpi_status status;
  2570. union acpi_object *pss;
  2571. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  2572. struct acpi_processor *pr = per_cpu(processors, i);
  2573. if (!pr)
  2574. continue;
  2575. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  2576. if (ACPI_FAILURE(status))
  2577. continue;
  2578. pss = buffer.pointer;
  2579. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  2580. kfree(pss);
  2581. return false;
  2582. }
  2583. kfree(pss);
  2584. }
  2585. pr_debug("ACPI _PSS not found\n");
  2586. return true;
  2587. }
  2588. static bool __init intel_pstate_no_acpi_pcch(void)
  2589. {
  2590. acpi_status status;
  2591. acpi_handle handle;
  2592. status = acpi_get_handle(NULL, "\\_SB", &handle);
  2593. if (ACPI_FAILURE(status))
  2594. goto not_found;
  2595. if (acpi_has_method(handle, "PCCH"))
  2596. return false;
  2597. not_found:
  2598. pr_debug("ACPI PCCH not found\n");
  2599. return true;
  2600. }
  2601. static bool __init intel_pstate_has_acpi_ppc(void)
  2602. {
  2603. int i;
  2604. for_each_possible_cpu(i) {
  2605. struct acpi_processor *pr = per_cpu(processors, i);
  2606. if (!pr)
  2607. continue;
  2608. if (acpi_has_method(pr->handle, "_PPC"))
  2609. return true;
  2610. }
  2611. pr_debug("ACPI _PPC not found\n");
  2612. return false;
  2613. }
  2614. enum {
  2615. PSS,
  2616. PPC,
  2617. };
  2618. /* Hardware vendor-specific info that has its own power management modes */
  2619. static struct acpi_platform_list plat_info[] __initdata = {
  2620. {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
  2621. {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2622. {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2623. {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2624. {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2625. {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2626. {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2627. {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2628. {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2629. {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2630. {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2631. {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2632. {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2633. {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2634. {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
  2635. { } /* End */
  2636. };
  2637. #define BITMASK_OOB (BIT(8) | BIT(18))
  2638. static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
  2639. {
  2640. const struct x86_cpu_id *id;
  2641. u64 misc_pwr;
  2642. int idx;
  2643. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  2644. if (id) {
  2645. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  2646. if (misc_pwr & BITMASK_OOB) {
  2647. pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
  2648. pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
  2649. return true;
  2650. }
  2651. }
  2652. idx = acpi_match_platform_list(plat_info);
  2653. if (idx < 0)
  2654. return false;
  2655. switch (plat_info[idx].data) {
  2656. case PSS:
  2657. if (!intel_pstate_no_acpi_pss())
  2658. return false;
  2659. return intel_pstate_no_acpi_pcch();
  2660. case PPC:
  2661. return intel_pstate_has_acpi_ppc() && !force_load;
  2662. }
  2663. return false;
  2664. }
  2665. static void intel_pstate_request_control_from_smm(void)
  2666. {
  2667. /*
  2668. * It may be unsafe to request P-states control from SMM if _PPC support
  2669. * has not been enabled.
  2670. */
  2671. if (acpi_ppc)
  2672. acpi_processor_pstate_control();
  2673. }
  2674. #else /* CONFIG_ACPI not enabled */
  2675. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  2676. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  2677. static inline void intel_pstate_request_control_from_smm(void) {}
  2678. #endif /* CONFIG_ACPI */
  2679. #define INTEL_PSTATE_HWP_BROADWELL 0x01
  2680. #define X86_MATCH_HWP(model, hwp_mode) \
  2681. X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
  2682. X86_FEATURE_HWP, hwp_mode)
  2683. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  2684. X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
  2685. X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
  2686. X86_MATCH_HWP(ANY, 0),
  2687. {}
  2688. };
  2689. static bool intel_pstate_hwp_is_enabled(void)
  2690. {
  2691. u64 value;
  2692. rdmsrl(MSR_PM_ENABLE, value);
  2693. return !!(value & 0x1);
  2694. }
  2695. static const struct x86_cpu_id intel_epp_balance_perf[] = {
  2696. /*
  2697. * Set EPP value as 102, this is the max suggested EPP
  2698. * which can result in one core turbo frequency for
  2699. * AlderLake Mobile CPUs.
  2700. */
  2701. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, 102),
  2702. {}
  2703. };
  2704. static int __init intel_pstate_init(void)
  2705. {
  2706. static struct cpudata **_all_cpu_data;
  2707. const struct x86_cpu_id *id;
  2708. int rc;
  2709. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  2710. return -ENODEV;
  2711. id = x86_match_cpu(hwp_support_ids);
  2712. if (id) {
  2713. bool hwp_forced = intel_pstate_hwp_is_enabled();
  2714. if (hwp_forced)
  2715. pr_info("HWP enabled by BIOS\n");
  2716. else if (no_load)
  2717. return -ENODEV;
  2718. copy_cpu_funcs(&core_funcs);
  2719. /*
  2720. * Avoid enabling HWP for processors without EPP support,
  2721. * because that means incomplete HWP implementation which is a
  2722. * corner case and supporting it is generally problematic.
  2723. *
  2724. * If HWP is enabled already, though, there is no choice but to
  2725. * deal with it.
  2726. */
  2727. if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
  2728. WRITE_ONCE(hwp_active, 1);
  2729. hwp_mode_bdw = id->driver_data;
  2730. intel_pstate.attr = hwp_cpufreq_attrs;
  2731. intel_cpufreq.attr = hwp_cpufreq_attrs;
  2732. intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
  2733. intel_cpufreq.adjust_perf = intel_cpufreq_adjust_perf;
  2734. if (!default_driver)
  2735. default_driver = &intel_pstate;
  2736. if (boot_cpu_has(X86_FEATURE_HYBRID_CPU))
  2737. pstate_funcs.get_cpu_scaling = hybrid_get_cpu_scaling;
  2738. goto hwp_cpu_matched;
  2739. }
  2740. pr_info("HWP not enabled\n");
  2741. } else {
  2742. if (no_load)
  2743. return -ENODEV;
  2744. id = x86_match_cpu(intel_pstate_cpu_ids);
  2745. if (!id) {
  2746. pr_info("CPU model not supported\n");
  2747. return -ENODEV;
  2748. }
  2749. copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
  2750. }
  2751. if (intel_pstate_msrs_not_valid()) {
  2752. pr_info("Invalid MSRs\n");
  2753. return -ENODEV;
  2754. }
  2755. /* Without HWP start in the passive mode. */
  2756. if (!default_driver)
  2757. default_driver = &intel_cpufreq;
  2758. hwp_cpu_matched:
  2759. /*
  2760. * The Intel pstate driver will be ignored if the platform
  2761. * firmware has its own power management modes.
  2762. */
  2763. if (intel_pstate_platform_pwr_mgmt_exists()) {
  2764. pr_info("P-states controlled by the platform\n");
  2765. return -ENODEV;
  2766. }
  2767. if (!hwp_active && hwp_only)
  2768. return -ENOTSUPP;
  2769. pr_info("Intel P-state driver initializing\n");
  2770. _all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
  2771. if (!_all_cpu_data)
  2772. return -ENOMEM;
  2773. WRITE_ONCE(all_cpu_data, _all_cpu_data);
  2774. intel_pstate_request_control_from_smm();
  2775. intel_pstate_sysfs_expose_params();
  2776. if (hwp_active) {
  2777. const struct x86_cpu_id *id = x86_match_cpu(intel_epp_balance_perf);
  2778. if (id)
  2779. epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = id->driver_data;
  2780. }
  2781. mutex_lock(&intel_pstate_driver_lock);
  2782. rc = intel_pstate_register_driver(default_driver);
  2783. mutex_unlock(&intel_pstate_driver_lock);
  2784. if (rc) {
  2785. intel_pstate_sysfs_remove();
  2786. return rc;
  2787. }
  2788. if (hwp_active) {
  2789. const struct x86_cpu_id *id;
  2790. id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
  2791. if (id) {
  2792. set_power_ctl_ee_state(false);
  2793. pr_info("Disabling energy efficiency optimization\n");
  2794. }
  2795. pr_info("HWP enabled\n");
  2796. } else if (boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
  2797. pr_warn("Problematic setup: Hybrid processor with disabled HWP\n");
  2798. }
  2799. return 0;
  2800. }
  2801. device_initcall(intel_pstate_init);
  2802. static int __init intel_pstate_setup(char *str)
  2803. {
  2804. if (!str)
  2805. return -EINVAL;
  2806. if (!strcmp(str, "disable"))
  2807. no_load = 1;
  2808. else if (!strcmp(str, "active"))
  2809. default_driver = &intel_pstate;
  2810. else if (!strcmp(str, "passive"))
  2811. default_driver = &intel_cpufreq;
  2812. if (!strcmp(str, "no_hwp"))
  2813. no_hwp = 1;
  2814. if (!strcmp(str, "force"))
  2815. force_load = 1;
  2816. if (!strcmp(str, "hwp_only"))
  2817. hwp_only = 1;
  2818. if (!strcmp(str, "per_cpu_perf_limits"))
  2819. per_cpu_limits = true;
  2820. #ifdef CONFIG_ACPI
  2821. if (!strcmp(str, "support_acpi_ppc"))
  2822. acpi_ppc = true;
  2823. #endif
  2824. return 0;
  2825. }
  2826. early_param("intel_pstate", intel_pstate_setup);
  2827. MODULE_AUTHOR("Dirk Brandewie <[email protected]>");
  2828. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  2829. MODULE_LICENSE("GPL");