intel_pstate.c 64 KB

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