intel_pstate.c 98 KB

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