intel_pstate.c 65 KB

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