intel_pstate.c 101 KB

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