intel_pstate.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  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. *
  204. * This structure stores per CPU instance data for all CPUs.
  205. */
  206. struct cpudata {
  207. int cpu;
  208. unsigned int policy;
  209. struct update_util_data update_util;
  210. bool update_util_set;
  211. struct pstate_data pstate;
  212. struct vid_data vid;
  213. u64 last_update;
  214. u64 last_sample_time;
  215. u64 aperf_mperf_shift;
  216. u64 prev_aperf;
  217. u64 prev_mperf;
  218. u64 prev_tsc;
  219. u64 prev_cummulative_iowait;
  220. struct sample sample;
  221. int32_t min_perf_ratio;
  222. int32_t max_perf_ratio;
  223. #ifdef CONFIG_ACPI
  224. struct acpi_processor_performance acpi_perf_data;
  225. bool valid_pss_table;
  226. #endif
  227. unsigned int iowait_boost;
  228. s16 epp_powersave;
  229. s16 epp_policy;
  230. s16 epp_default;
  231. s16 epp_saved;
  232. };
  233. static struct cpudata **all_cpu_data;
  234. /**
  235. * struct pstate_funcs - Per CPU model specific callbacks
  236. * @get_max: Callback to get maximum non turbo effective P state
  237. * @get_max_physical: Callback to get maximum non turbo physical P state
  238. * @get_min: Callback to get minimum P state
  239. * @get_turbo: Callback to get turbo P state
  240. * @get_scaling: Callback to get frequency scaling factor
  241. * @get_val: Callback to convert P state to actual MSR write value
  242. * @get_vid: Callback to get VID data for Atom platforms
  243. *
  244. * Core and Atom CPU models have different way to get P State limits. This
  245. * structure is used to store those callbacks.
  246. */
  247. struct pstate_funcs {
  248. int (*get_max)(void);
  249. int (*get_max_physical)(void);
  250. int (*get_min)(void);
  251. int (*get_turbo)(void);
  252. int (*get_scaling)(void);
  253. int (*get_aperf_mperf_shift)(void);
  254. u64 (*get_val)(struct cpudata*, int pstate);
  255. void (*get_vid)(struct cpudata *);
  256. };
  257. static struct pstate_funcs pstate_funcs __read_mostly;
  258. static int hwp_active __read_mostly;
  259. static bool per_cpu_limits __read_mostly;
  260. static struct cpufreq_driver *intel_pstate_driver __read_mostly;
  261. #ifdef CONFIG_ACPI
  262. static bool acpi_ppc;
  263. #endif
  264. static struct global_params global;
  265. static DEFINE_MUTEX(intel_pstate_driver_lock);
  266. static DEFINE_MUTEX(intel_pstate_limits_lock);
  267. #ifdef CONFIG_ACPI
  268. static bool intel_pstate_get_ppc_enable_status(void)
  269. {
  270. if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
  271. acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
  272. return true;
  273. return acpi_ppc;
  274. }
  275. #ifdef CONFIG_ACPI_CPPC_LIB
  276. /* The work item is needed to avoid CPU hotplug locking issues */
  277. static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
  278. {
  279. sched_set_itmt_support();
  280. }
  281. static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
  282. static void intel_pstate_set_itmt_prio(int cpu)
  283. {
  284. struct cppc_perf_caps cppc_perf;
  285. static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
  286. int ret;
  287. ret = cppc_get_perf_caps(cpu, &cppc_perf);
  288. if (ret)
  289. return;
  290. /*
  291. * The priorities can be set regardless of whether or not
  292. * sched_set_itmt_support(true) has been called and it is valid to
  293. * update them at any time after it has been called.
  294. */
  295. sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
  296. if (max_highest_perf <= min_highest_perf) {
  297. if (cppc_perf.highest_perf > max_highest_perf)
  298. max_highest_perf = cppc_perf.highest_perf;
  299. if (cppc_perf.highest_perf < min_highest_perf)
  300. min_highest_perf = cppc_perf.highest_perf;
  301. if (max_highest_perf > min_highest_perf) {
  302. /*
  303. * This code can be run during CPU online under the
  304. * CPU hotplug locks, so sched_set_itmt_support()
  305. * cannot be called from here. Queue up a work item
  306. * to invoke it.
  307. */
  308. schedule_work(&sched_itmt_work);
  309. }
  310. }
  311. }
  312. #else
  313. static void intel_pstate_set_itmt_prio(int cpu)
  314. {
  315. }
  316. #endif
  317. static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  318. {
  319. struct cpudata *cpu;
  320. int ret;
  321. int i;
  322. if (hwp_active) {
  323. intel_pstate_set_itmt_prio(policy->cpu);
  324. return;
  325. }
  326. if (!intel_pstate_get_ppc_enable_status())
  327. return;
  328. cpu = all_cpu_data[policy->cpu];
  329. ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
  330. policy->cpu);
  331. if (ret)
  332. return;
  333. /*
  334. * Check if the control value in _PSS is for PERF_CTL MSR, which should
  335. * guarantee that the states returned by it map to the states in our
  336. * list directly.
  337. */
  338. if (cpu->acpi_perf_data.control_register.space_id !=
  339. ACPI_ADR_SPACE_FIXED_HARDWARE)
  340. goto err;
  341. /*
  342. * If there is only one entry _PSS, simply ignore _PSS and continue as
  343. * usual without taking _PSS into account
  344. */
  345. if (cpu->acpi_perf_data.state_count < 2)
  346. goto err;
  347. pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
  348. for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
  349. pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
  350. (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
  351. (u32) cpu->acpi_perf_data.states[i].core_frequency,
  352. (u32) cpu->acpi_perf_data.states[i].power,
  353. (u32) cpu->acpi_perf_data.states[i].control);
  354. }
  355. /*
  356. * The _PSS table doesn't contain whole turbo frequency range.
  357. * This just contains +1 MHZ above the max non turbo frequency,
  358. * with control value corresponding to max turbo ratio. But
  359. * when cpufreq set policy is called, it will call with this
  360. * max frequency, which will cause a reduced performance as
  361. * this driver uses real max turbo frequency as the max
  362. * frequency. So correct this frequency in _PSS table to
  363. * correct max turbo frequency based on the turbo state.
  364. * Also need to convert to MHz as _PSS freq is in MHz.
  365. */
  366. if (!global.turbo_disabled)
  367. cpu->acpi_perf_data.states[0].core_frequency =
  368. policy->cpuinfo.max_freq / 1000;
  369. cpu->valid_pss_table = true;
  370. pr_debug("_PPC limits will be enforced\n");
  371. return;
  372. err:
  373. cpu->valid_pss_table = false;
  374. acpi_processor_unregister_performance(policy->cpu);
  375. }
  376. static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  377. {
  378. struct cpudata *cpu;
  379. cpu = all_cpu_data[policy->cpu];
  380. if (!cpu->valid_pss_table)
  381. return;
  382. acpi_processor_unregister_performance(policy->cpu);
  383. }
  384. #else
  385. static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  386. {
  387. }
  388. static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  389. {
  390. }
  391. #endif
  392. static inline void update_turbo_state(void)
  393. {
  394. u64 misc_en;
  395. struct cpudata *cpu;
  396. cpu = all_cpu_data[0];
  397. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  398. global.turbo_disabled =
  399. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  400. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  401. }
  402. static int min_perf_pct_min(void)
  403. {
  404. struct cpudata *cpu = all_cpu_data[0];
  405. int turbo_pstate = cpu->pstate.turbo_pstate;
  406. return turbo_pstate ?
  407. (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
  408. }
  409. static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
  410. {
  411. u64 epb;
  412. int ret;
  413. if (!static_cpu_has(X86_FEATURE_EPB))
  414. return -ENXIO;
  415. ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  416. if (ret)
  417. return (s16)ret;
  418. return (s16)(epb & 0x0f);
  419. }
  420. static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
  421. {
  422. s16 epp;
  423. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  424. /*
  425. * When hwp_req_data is 0, means that caller didn't read
  426. * MSR_HWP_REQUEST, so need to read and get EPP.
  427. */
  428. if (!hwp_req_data) {
  429. epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
  430. &hwp_req_data);
  431. if (epp)
  432. return epp;
  433. }
  434. epp = (hwp_req_data >> 24) & 0xff;
  435. } else {
  436. /* When there is no EPP present, HWP uses EPB settings */
  437. epp = intel_pstate_get_epb(cpu_data);
  438. }
  439. return epp;
  440. }
  441. static int intel_pstate_set_epb(int cpu, s16 pref)
  442. {
  443. u64 epb;
  444. int ret;
  445. if (!static_cpu_has(X86_FEATURE_EPB))
  446. return -ENXIO;
  447. ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
  448. if (ret)
  449. return ret;
  450. epb = (epb & ~0x0f) | pref;
  451. wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
  452. return 0;
  453. }
  454. /*
  455. * EPP/EPB display strings corresponding to EPP index in the
  456. * energy_perf_strings[]
  457. * index String
  458. *-------------------------------------
  459. * 0 default
  460. * 1 performance
  461. * 2 balance_performance
  462. * 3 balance_power
  463. * 4 power
  464. */
  465. static const char * const energy_perf_strings[] = {
  466. "default",
  467. "performance",
  468. "balance_performance",
  469. "balance_power",
  470. "power",
  471. NULL
  472. };
  473. static const unsigned int epp_values[] = {
  474. HWP_EPP_PERFORMANCE,
  475. HWP_EPP_BALANCE_PERFORMANCE,
  476. HWP_EPP_BALANCE_POWERSAVE,
  477. HWP_EPP_POWERSAVE
  478. };
  479. static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
  480. {
  481. s16 epp;
  482. int index = -EINVAL;
  483. epp = intel_pstate_get_epp(cpu_data, 0);
  484. if (epp < 0)
  485. return epp;
  486. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  487. if (epp == HWP_EPP_PERFORMANCE)
  488. return 1;
  489. if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
  490. return 2;
  491. if (epp <= HWP_EPP_BALANCE_POWERSAVE)
  492. return 3;
  493. else
  494. return 4;
  495. } else if (static_cpu_has(X86_FEATURE_EPB)) {
  496. /*
  497. * Range:
  498. * 0x00-0x03 : Performance
  499. * 0x04-0x07 : Balance performance
  500. * 0x08-0x0B : Balance power
  501. * 0x0C-0x0F : Power
  502. * The EPB is a 4 bit value, but our ranges restrict the
  503. * value which can be set. Here only using top two bits
  504. * effectively.
  505. */
  506. index = (epp >> 2) + 1;
  507. }
  508. return index;
  509. }
  510. static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
  511. int pref_index)
  512. {
  513. int epp = -EINVAL;
  514. int ret;
  515. if (!pref_index)
  516. epp = cpu_data->epp_default;
  517. mutex_lock(&intel_pstate_limits_lock);
  518. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  519. u64 value;
  520. ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
  521. if (ret)
  522. goto return_pref;
  523. value &= ~GENMASK_ULL(31, 24);
  524. if (epp == -EINVAL)
  525. epp = epp_values[pref_index - 1];
  526. value |= (u64)epp << 24;
  527. ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
  528. } else {
  529. if (epp == -EINVAL)
  530. epp = (pref_index - 1) << 2;
  531. ret = intel_pstate_set_epb(cpu_data->cpu, epp);
  532. }
  533. return_pref:
  534. mutex_unlock(&intel_pstate_limits_lock);
  535. return ret;
  536. }
  537. static ssize_t show_energy_performance_available_preferences(
  538. struct cpufreq_policy *policy, char *buf)
  539. {
  540. int i = 0;
  541. int ret = 0;
  542. while (energy_perf_strings[i] != NULL)
  543. ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
  544. ret += sprintf(&buf[ret], "\n");
  545. return ret;
  546. }
  547. cpufreq_freq_attr_ro(energy_performance_available_preferences);
  548. static ssize_t store_energy_performance_preference(
  549. struct cpufreq_policy *policy, const char *buf, size_t count)
  550. {
  551. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  552. char str_preference[21];
  553. int ret, i = 0;
  554. ret = sscanf(buf, "%20s", str_preference);
  555. if (ret != 1)
  556. return -EINVAL;
  557. while (energy_perf_strings[i] != NULL) {
  558. if (!strcmp(str_preference, energy_perf_strings[i])) {
  559. intel_pstate_set_energy_pref_index(cpu_data, i);
  560. return count;
  561. }
  562. ++i;
  563. }
  564. return -EINVAL;
  565. }
  566. static ssize_t show_energy_performance_preference(
  567. struct cpufreq_policy *policy, char *buf)
  568. {
  569. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  570. int preference;
  571. preference = intel_pstate_get_energy_pref_index(cpu_data);
  572. if (preference < 0)
  573. return preference;
  574. return sprintf(buf, "%s\n", energy_perf_strings[preference]);
  575. }
  576. cpufreq_freq_attr_rw(energy_performance_preference);
  577. static struct freq_attr *hwp_cpufreq_attrs[] = {
  578. &energy_performance_preference,
  579. &energy_performance_available_preferences,
  580. NULL,
  581. };
  582. static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
  583. int *current_max)
  584. {
  585. u64 cap;
  586. rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
  587. if (global.no_turbo)
  588. *current_max = HWP_GUARANTEED_PERF(cap);
  589. else
  590. *current_max = HWP_HIGHEST_PERF(cap);
  591. *phy_max = HWP_HIGHEST_PERF(cap);
  592. }
  593. static void intel_pstate_hwp_set(unsigned int cpu)
  594. {
  595. struct cpudata *cpu_data = all_cpu_data[cpu];
  596. int max, min;
  597. u64 value;
  598. s16 epp;
  599. max = cpu_data->max_perf_ratio;
  600. min = cpu_data->min_perf_ratio;
  601. if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
  602. min = max;
  603. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  604. value &= ~HWP_MIN_PERF(~0L);
  605. value |= HWP_MIN_PERF(min);
  606. value &= ~HWP_MAX_PERF(~0L);
  607. value |= HWP_MAX_PERF(max);
  608. if (cpu_data->epp_policy == cpu_data->policy)
  609. goto skip_epp;
  610. cpu_data->epp_policy = cpu_data->policy;
  611. if (cpu_data->epp_saved >= 0) {
  612. epp = cpu_data->epp_saved;
  613. cpu_data->epp_saved = -EINVAL;
  614. goto update_epp;
  615. }
  616. if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
  617. epp = intel_pstate_get_epp(cpu_data, value);
  618. cpu_data->epp_powersave = epp;
  619. /* If EPP read was failed, then don't try to write */
  620. if (epp < 0)
  621. goto skip_epp;
  622. epp = 0;
  623. } else {
  624. /* skip setting EPP, when saved value is invalid */
  625. if (cpu_data->epp_powersave < 0)
  626. goto skip_epp;
  627. /*
  628. * No need to restore EPP when it is not zero. This
  629. * means:
  630. * - Policy is not changed
  631. * - user has manually changed
  632. * - Error reading EPB
  633. */
  634. epp = intel_pstate_get_epp(cpu_data, value);
  635. if (epp)
  636. goto skip_epp;
  637. epp = cpu_data->epp_powersave;
  638. }
  639. update_epp:
  640. if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
  641. value &= ~GENMASK_ULL(31, 24);
  642. value |= (u64)epp << 24;
  643. } else {
  644. intel_pstate_set_epb(cpu, epp);
  645. }
  646. skip_epp:
  647. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  648. }
  649. static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
  650. {
  651. struct cpudata *cpu_data = all_cpu_data[policy->cpu];
  652. if (!hwp_active)
  653. return 0;
  654. cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
  655. return 0;
  656. }
  657. static void intel_pstate_hwp_enable(struct cpudata *cpudata);
  658. static int intel_pstate_resume(struct cpufreq_policy *policy)
  659. {
  660. if (!hwp_active)
  661. return 0;
  662. mutex_lock(&intel_pstate_limits_lock);
  663. if (policy->cpu == 0)
  664. intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
  665. all_cpu_data[policy->cpu]->epp_policy = 0;
  666. intel_pstate_hwp_set(policy->cpu);
  667. mutex_unlock(&intel_pstate_limits_lock);
  668. return 0;
  669. }
  670. static void intel_pstate_update_policies(void)
  671. {
  672. int cpu;
  673. for_each_possible_cpu(cpu)
  674. cpufreq_update_policy(cpu);
  675. }
  676. /************************** sysfs begin ************************/
  677. #define show_one(file_name, object) \
  678. static ssize_t show_##file_name \
  679. (struct kobject *kobj, struct attribute *attr, char *buf) \
  680. { \
  681. return sprintf(buf, "%u\n", global.object); \
  682. }
  683. static ssize_t intel_pstate_show_status(char *buf);
  684. static int intel_pstate_update_status(const char *buf, size_t size);
  685. static ssize_t show_status(struct kobject *kobj,
  686. struct attribute *attr, char *buf)
  687. {
  688. ssize_t ret;
  689. mutex_lock(&intel_pstate_driver_lock);
  690. ret = intel_pstate_show_status(buf);
  691. mutex_unlock(&intel_pstate_driver_lock);
  692. return ret;
  693. }
  694. static ssize_t store_status(struct kobject *a, struct attribute *b,
  695. const char *buf, size_t count)
  696. {
  697. char *p = memchr(buf, '\n', count);
  698. int ret;
  699. mutex_lock(&intel_pstate_driver_lock);
  700. ret = intel_pstate_update_status(buf, p ? p - buf : count);
  701. mutex_unlock(&intel_pstate_driver_lock);
  702. return ret < 0 ? ret : count;
  703. }
  704. static ssize_t show_turbo_pct(struct kobject *kobj,
  705. struct attribute *attr, char *buf)
  706. {
  707. struct cpudata *cpu;
  708. int total, no_turbo, turbo_pct;
  709. uint32_t turbo_fp;
  710. mutex_lock(&intel_pstate_driver_lock);
  711. if (!intel_pstate_driver) {
  712. mutex_unlock(&intel_pstate_driver_lock);
  713. return -EAGAIN;
  714. }
  715. cpu = all_cpu_data[0];
  716. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  717. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  718. turbo_fp = div_fp(no_turbo, total);
  719. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  720. mutex_unlock(&intel_pstate_driver_lock);
  721. return sprintf(buf, "%u\n", turbo_pct);
  722. }
  723. static ssize_t show_num_pstates(struct kobject *kobj,
  724. struct attribute *attr, char *buf)
  725. {
  726. struct cpudata *cpu;
  727. int total;
  728. mutex_lock(&intel_pstate_driver_lock);
  729. if (!intel_pstate_driver) {
  730. mutex_unlock(&intel_pstate_driver_lock);
  731. return -EAGAIN;
  732. }
  733. cpu = all_cpu_data[0];
  734. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  735. mutex_unlock(&intel_pstate_driver_lock);
  736. return sprintf(buf, "%u\n", total);
  737. }
  738. static ssize_t show_no_turbo(struct kobject *kobj,
  739. struct attribute *attr, char *buf)
  740. {
  741. ssize_t ret;
  742. mutex_lock(&intel_pstate_driver_lock);
  743. if (!intel_pstate_driver) {
  744. mutex_unlock(&intel_pstate_driver_lock);
  745. return -EAGAIN;
  746. }
  747. update_turbo_state();
  748. if (global.turbo_disabled)
  749. ret = sprintf(buf, "%u\n", global.turbo_disabled);
  750. else
  751. ret = sprintf(buf, "%u\n", global.no_turbo);
  752. mutex_unlock(&intel_pstate_driver_lock);
  753. return ret;
  754. }
  755. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  756. const char *buf, size_t count)
  757. {
  758. unsigned int input;
  759. int ret;
  760. ret = sscanf(buf, "%u", &input);
  761. if (ret != 1)
  762. return -EINVAL;
  763. mutex_lock(&intel_pstate_driver_lock);
  764. if (!intel_pstate_driver) {
  765. mutex_unlock(&intel_pstate_driver_lock);
  766. return -EAGAIN;
  767. }
  768. mutex_lock(&intel_pstate_limits_lock);
  769. update_turbo_state();
  770. if (global.turbo_disabled) {
  771. pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
  772. mutex_unlock(&intel_pstate_limits_lock);
  773. mutex_unlock(&intel_pstate_driver_lock);
  774. return -EPERM;
  775. }
  776. global.no_turbo = clamp_t(int, input, 0, 1);
  777. if (global.no_turbo) {
  778. struct cpudata *cpu = all_cpu_data[0];
  779. int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
  780. /* Squash the global minimum into the permitted range. */
  781. if (global.min_perf_pct > pct)
  782. global.min_perf_pct = pct;
  783. }
  784. mutex_unlock(&intel_pstate_limits_lock);
  785. intel_pstate_update_policies();
  786. mutex_unlock(&intel_pstate_driver_lock);
  787. return count;
  788. }
  789. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  790. const char *buf, size_t count)
  791. {
  792. unsigned int input;
  793. int ret;
  794. ret = sscanf(buf, "%u", &input);
  795. if (ret != 1)
  796. return -EINVAL;
  797. mutex_lock(&intel_pstate_driver_lock);
  798. if (!intel_pstate_driver) {
  799. mutex_unlock(&intel_pstate_driver_lock);
  800. return -EAGAIN;
  801. }
  802. mutex_lock(&intel_pstate_limits_lock);
  803. global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
  804. mutex_unlock(&intel_pstate_limits_lock);
  805. intel_pstate_update_policies();
  806. mutex_unlock(&intel_pstate_driver_lock);
  807. return count;
  808. }
  809. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  810. const char *buf, size_t count)
  811. {
  812. unsigned int input;
  813. int ret;
  814. ret = sscanf(buf, "%u", &input);
  815. if (ret != 1)
  816. return -EINVAL;
  817. mutex_lock(&intel_pstate_driver_lock);
  818. if (!intel_pstate_driver) {
  819. mutex_unlock(&intel_pstate_driver_lock);
  820. return -EAGAIN;
  821. }
  822. mutex_lock(&intel_pstate_limits_lock);
  823. global.min_perf_pct = clamp_t(int, input,
  824. min_perf_pct_min(), global.max_perf_pct);
  825. mutex_unlock(&intel_pstate_limits_lock);
  826. intel_pstate_update_policies();
  827. mutex_unlock(&intel_pstate_driver_lock);
  828. return count;
  829. }
  830. show_one(max_perf_pct, max_perf_pct);
  831. show_one(min_perf_pct, min_perf_pct);
  832. define_one_global_rw(status);
  833. define_one_global_rw(no_turbo);
  834. define_one_global_rw(max_perf_pct);
  835. define_one_global_rw(min_perf_pct);
  836. define_one_global_ro(turbo_pct);
  837. define_one_global_ro(num_pstates);
  838. static struct attribute *intel_pstate_attributes[] = {
  839. &status.attr,
  840. &no_turbo.attr,
  841. &turbo_pct.attr,
  842. &num_pstates.attr,
  843. NULL
  844. };
  845. static const struct attribute_group intel_pstate_attr_group = {
  846. .attrs = intel_pstate_attributes,
  847. };
  848. static void __init intel_pstate_sysfs_expose_params(void)
  849. {
  850. struct kobject *intel_pstate_kobject;
  851. int rc;
  852. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  853. &cpu_subsys.dev_root->kobj);
  854. if (WARN_ON(!intel_pstate_kobject))
  855. return;
  856. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  857. if (WARN_ON(rc))
  858. return;
  859. /*
  860. * If per cpu limits are enforced there are no global limits, so
  861. * return without creating max/min_perf_pct attributes
  862. */
  863. if (per_cpu_limits)
  864. return;
  865. rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
  866. WARN_ON(rc);
  867. rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
  868. WARN_ON(rc);
  869. }
  870. /************************** sysfs end ************************/
  871. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  872. {
  873. /* First disable HWP notification interrupt as we don't process them */
  874. if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
  875. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  876. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  877. cpudata->epp_policy = 0;
  878. if (cpudata->epp_default == -EINVAL)
  879. cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
  880. }
  881. #define MSR_IA32_POWER_CTL_BIT_EE 19
  882. /* Disable energy efficiency optimization */
  883. static void intel_pstate_disable_ee(int cpu)
  884. {
  885. u64 power_ctl;
  886. int ret;
  887. ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
  888. if (ret)
  889. return;
  890. if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
  891. pr_info("Disabling energy efficiency optimization\n");
  892. power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
  893. wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
  894. }
  895. }
  896. static int atom_get_min_pstate(void)
  897. {
  898. u64 value;
  899. rdmsrl(MSR_ATOM_CORE_RATIOS, value);
  900. return (value >> 8) & 0x7F;
  901. }
  902. static int atom_get_max_pstate(void)
  903. {
  904. u64 value;
  905. rdmsrl(MSR_ATOM_CORE_RATIOS, value);
  906. return (value >> 16) & 0x7F;
  907. }
  908. static int atom_get_turbo_pstate(void)
  909. {
  910. u64 value;
  911. rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
  912. return value & 0x7F;
  913. }
  914. static u64 atom_get_val(struct cpudata *cpudata, int pstate)
  915. {
  916. u64 val;
  917. int32_t vid_fp;
  918. u32 vid;
  919. val = (u64)pstate << 8;
  920. if (global.no_turbo && !global.turbo_disabled)
  921. val |= (u64)1 << 32;
  922. vid_fp = cpudata->vid.min + mul_fp(
  923. int_tofp(pstate - cpudata->pstate.min_pstate),
  924. cpudata->vid.ratio);
  925. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  926. vid = ceiling_fp(vid_fp);
  927. if (pstate > cpudata->pstate.max_pstate)
  928. vid = cpudata->vid.turbo;
  929. return val | vid;
  930. }
  931. static int silvermont_get_scaling(void)
  932. {
  933. u64 value;
  934. int i;
  935. /* Defined in Table 35-6 from SDM (Sept 2015) */
  936. static int silvermont_freq_table[] = {
  937. 83300, 100000, 133300, 116700, 80000};
  938. rdmsrl(MSR_FSB_FREQ, value);
  939. i = value & 0x7;
  940. WARN_ON(i > 4);
  941. return silvermont_freq_table[i];
  942. }
  943. static int airmont_get_scaling(void)
  944. {
  945. u64 value;
  946. int i;
  947. /* Defined in Table 35-10 from SDM (Sept 2015) */
  948. static int airmont_freq_table[] = {
  949. 83300, 100000, 133300, 116700, 80000,
  950. 93300, 90000, 88900, 87500};
  951. rdmsrl(MSR_FSB_FREQ, value);
  952. i = value & 0xF;
  953. WARN_ON(i > 8);
  954. return airmont_freq_table[i];
  955. }
  956. static void atom_get_vid(struct cpudata *cpudata)
  957. {
  958. u64 value;
  959. rdmsrl(MSR_ATOM_CORE_VIDS, value);
  960. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  961. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  962. cpudata->vid.ratio = div_fp(
  963. cpudata->vid.max - cpudata->vid.min,
  964. int_tofp(cpudata->pstate.max_pstate -
  965. cpudata->pstate.min_pstate));
  966. rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
  967. cpudata->vid.turbo = value & 0x7f;
  968. }
  969. static int core_get_min_pstate(void)
  970. {
  971. u64 value;
  972. rdmsrl(MSR_PLATFORM_INFO, value);
  973. return (value >> 40) & 0xFF;
  974. }
  975. static int core_get_max_pstate_physical(void)
  976. {
  977. u64 value;
  978. rdmsrl(MSR_PLATFORM_INFO, value);
  979. return (value >> 8) & 0xFF;
  980. }
  981. static int core_get_tdp_ratio(u64 plat_info)
  982. {
  983. /* Check how many TDP levels present */
  984. if (plat_info & 0x600000000) {
  985. u64 tdp_ctrl;
  986. u64 tdp_ratio;
  987. int tdp_msr;
  988. int err;
  989. /* Get the TDP level (0, 1, 2) to get ratios */
  990. err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  991. if (err)
  992. return err;
  993. /* TDP MSR are continuous starting at 0x648 */
  994. tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
  995. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  996. if (err)
  997. return err;
  998. /* For level 1 and 2, bits[23:16] contain the ratio */
  999. if (tdp_ctrl & 0x03)
  1000. tdp_ratio >>= 16;
  1001. tdp_ratio &= 0xff; /* ratios are only 8 bits long */
  1002. pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
  1003. return (int)tdp_ratio;
  1004. }
  1005. return -ENXIO;
  1006. }
  1007. static int core_get_max_pstate(void)
  1008. {
  1009. u64 tar;
  1010. u64 plat_info;
  1011. int max_pstate;
  1012. int tdp_ratio;
  1013. int err;
  1014. rdmsrl(MSR_PLATFORM_INFO, plat_info);
  1015. max_pstate = (plat_info >> 8) & 0xFF;
  1016. tdp_ratio = core_get_tdp_ratio(plat_info);
  1017. if (tdp_ratio <= 0)
  1018. return max_pstate;
  1019. if (hwp_active) {
  1020. /* Turbo activation ratio is not used on HWP platforms */
  1021. return tdp_ratio;
  1022. }
  1023. err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
  1024. if (!err) {
  1025. int tar_levels;
  1026. /* Do some sanity checking for safety */
  1027. tar_levels = tar & 0xff;
  1028. if (tdp_ratio - 1 == tar_levels) {
  1029. max_pstate = tar_levels;
  1030. pr_debug("max_pstate=TAC %x\n", max_pstate);
  1031. }
  1032. }
  1033. return max_pstate;
  1034. }
  1035. static int core_get_turbo_pstate(void)
  1036. {
  1037. u64 value;
  1038. int nont, ret;
  1039. rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
  1040. nont = core_get_max_pstate();
  1041. ret = (value) & 255;
  1042. if (ret <= nont)
  1043. ret = nont;
  1044. return ret;
  1045. }
  1046. static inline int core_get_scaling(void)
  1047. {
  1048. return 100000;
  1049. }
  1050. static u64 core_get_val(struct cpudata *cpudata, int pstate)
  1051. {
  1052. u64 val;
  1053. val = (u64)pstate << 8;
  1054. if (global.no_turbo && !global.turbo_disabled)
  1055. val |= (u64)1 << 32;
  1056. return val;
  1057. }
  1058. static int knl_get_aperf_mperf_shift(void)
  1059. {
  1060. return 10;
  1061. }
  1062. static int knl_get_turbo_pstate(void)
  1063. {
  1064. u64 value;
  1065. int nont, ret;
  1066. rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
  1067. nont = core_get_max_pstate();
  1068. ret = (((value) >> 8) & 0xFF);
  1069. if (ret <= nont)
  1070. ret = nont;
  1071. return ret;
  1072. }
  1073. static int intel_pstate_get_base_pstate(struct cpudata *cpu)
  1074. {
  1075. return global.no_turbo || global.turbo_disabled ?
  1076. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1077. }
  1078. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
  1079. {
  1080. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  1081. cpu->pstate.current_pstate = pstate;
  1082. /*
  1083. * Generally, there is no guarantee that this code will always run on
  1084. * the CPU being updated, so force the register update to run on the
  1085. * right CPU.
  1086. */
  1087. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  1088. pstate_funcs.get_val(cpu, pstate));
  1089. }
  1090. static void intel_pstate_set_min_pstate(struct cpudata *cpu)
  1091. {
  1092. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
  1093. }
  1094. static void intel_pstate_max_within_limits(struct cpudata *cpu)
  1095. {
  1096. int pstate;
  1097. update_turbo_state();
  1098. pstate = intel_pstate_get_base_pstate(cpu);
  1099. pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
  1100. intel_pstate_set_pstate(cpu, pstate);
  1101. }
  1102. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  1103. {
  1104. cpu->pstate.min_pstate = pstate_funcs.get_min();
  1105. cpu->pstate.max_pstate = pstate_funcs.get_max();
  1106. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  1107. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  1108. cpu->pstate.scaling = pstate_funcs.get_scaling();
  1109. cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
  1110. cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1111. if (pstate_funcs.get_aperf_mperf_shift)
  1112. cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
  1113. if (pstate_funcs.get_vid)
  1114. pstate_funcs.get_vid(cpu);
  1115. intel_pstate_set_min_pstate(cpu);
  1116. }
  1117. static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
  1118. {
  1119. struct sample *sample = &cpu->sample;
  1120. sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
  1121. }
  1122. static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
  1123. {
  1124. u64 aperf, mperf;
  1125. unsigned long flags;
  1126. u64 tsc;
  1127. local_irq_save(flags);
  1128. rdmsrl(MSR_IA32_APERF, aperf);
  1129. rdmsrl(MSR_IA32_MPERF, mperf);
  1130. tsc = rdtsc();
  1131. if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
  1132. local_irq_restore(flags);
  1133. return false;
  1134. }
  1135. local_irq_restore(flags);
  1136. cpu->last_sample_time = cpu->sample.time;
  1137. cpu->sample.time = time;
  1138. cpu->sample.aperf = aperf;
  1139. cpu->sample.mperf = mperf;
  1140. cpu->sample.tsc = tsc;
  1141. cpu->sample.aperf -= cpu->prev_aperf;
  1142. cpu->sample.mperf -= cpu->prev_mperf;
  1143. cpu->sample.tsc -= cpu->prev_tsc;
  1144. cpu->prev_aperf = aperf;
  1145. cpu->prev_mperf = mperf;
  1146. cpu->prev_tsc = tsc;
  1147. /*
  1148. * First time this function is invoked in a given cycle, all of the
  1149. * previous sample data fields are equal to zero or stale and they must
  1150. * be populated with meaningful numbers for things to work, so assume
  1151. * that sample.time will always be reset before setting the utilization
  1152. * update hook and make the caller skip the sample then.
  1153. */
  1154. if (cpu->last_sample_time) {
  1155. intel_pstate_calc_avg_perf(cpu);
  1156. return true;
  1157. }
  1158. return false;
  1159. }
  1160. static inline int32_t get_avg_frequency(struct cpudata *cpu)
  1161. {
  1162. return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
  1163. }
  1164. static inline int32_t get_avg_pstate(struct cpudata *cpu)
  1165. {
  1166. return mul_ext_fp(cpu->pstate.max_pstate_physical,
  1167. cpu->sample.core_avg_perf);
  1168. }
  1169. static inline int32_t get_target_pstate(struct cpudata *cpu)
  1170. {
  1171. struct sample *sample = &cpu->sample;
  1172. int32_t busy_frac, boost;
  1173. int target, avg_pstate;
  1174. busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
  1175. sample->tsc);
  1176. boost = cpu->iowait_boost;
  1177. cpu->iowait_boost >>= 1;
  1178. if (busy_frac < boost)
  1179. busy_frac = boost;
  1180. sample->busy_scaled = busy_frac * 100;
  1181. target = global.no_turbo || global.turbo_disabled ?
  1182. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1183. target += target >> 2;
  1184. target = mul_fp(target, busy_frac);
  1185. if (target < cpu->pstate.min_pstate)
  1186. target = cpu->pstate.min_pstate;
  1187. /*
  1188. * If the average P-state during the previous cycle was higher than the
  1189. * current target, add 50% of the difference to the target to reduce
  1190. * possible performance oscillations and offset possible performance
  1191. * loss related to moving the workload from one CPU to another within
  1192. * a package/module.
  1193. */
  1194. avg_pstate = get_avg_pstate(cpu);
  1195. if (avg_pstate > target)
  1196. target += (avg_pstate - target) >> 1;
  1197. return target;
  1198. }
  1199. static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
  1200. {
  1201. int max_pstate = intel_pstate_get_base_pstate(cpu);
  1202. int min_pstate;
  1203. min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
  1204. max_pstate = max(min_pstate, cpu->max_perf_ratio);
  1205. return clamp_t(int, pstate, min_pstate, max_pstate);
  1206. }
  1207. static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
  1208. {
  1209. if (pstate == cpu->pstate.current_pstate)
  1210. return;
  1211. cpu->pstate.current_pstate = pstate;
  1212. wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
  1213. }
  1214. static void intel_pstate_adjust_pstate(struct cpudata *cpu)
  1215. {
  1216. int from = cpu->pstate.current_pstate;
  1217. struct sample *sample;
  1218. int target_pstate;
  1219. update_turbo_state();
  1220. target_pstate = get_target_pstate(cpu);
  1221. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  1222. trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
  1223. intel_pstate_update_pstate(cpu, target_pstate);
  1224. sample = &cpu->sample;
  1225. trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
  1226. fp_toint(sample->busy_scaled),
  1227. from,
  1228. cpu->pstate.current_pstate,
  1229. sample->mperf,
  1230. sample->aperf,
  1231. sample->tsc,
  1232. get_avg_frequency(cpu),
  1233. fp_toint(cpu->iowait_boost * 100));
  1234. }
  1235. static void intel_pstate_update_util(struct update_util_data *data, u64 time,
  1236. unsigned int flags)
  1237. {
  1238. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1239. u64 delta_ns;
  1240. /* Don't allow remote callbacks */
  1241. if (smp_processor_id() != cpu->cpu)
  1242. return;
  1243. if (flags & SCHED_CPUFREQ_IOWAIT) {
  1244. cpu->iowait_boost = int_tofp(1);
  1245. cpu->last_update = time;
  1246. /*
  1247. * The last time the busy was 100% so P-state was max anyway
  1248. * so avoid overhead of computation.
  1249. */
  1250. if (fp_toint(cpu->sample.busy_scaled) == 100)
  1251. return;
  1252. goto set_pstate;
  1253. } else if (cpu->iowait_boost) {
  1254. /* Clear iowait_boost if the CPU may have been idle. */
  1255. delta_ns = time - cpu->last_update;
  1256. if (delta_ns > TICK_NSEC)
  1257. cpu->iowait_boost = 0;
  1258. }
  1259. cpu->last_update = time;
  1260. delta_ns = time - cpu->sample.time;
  1261. if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
  1262. return;
  1263. set_pstate:
  1264. if (intel_pstate_sample(cpu, time))
  1265. intel_pstate_adjust_pstate(cpu);
  1266. }
  1267. static struct pstate_funcs core_funcs = {
  1268. .get_max = core_get_max_pstate,
  1269. .get_max_physical = core_get_max_pstate_physical,
  1270. .get_min = core_get_min_pstate,
  1271. .get_turbo = core_get_turbo_pstate,
  1272. .get_scaling = core_get_scaling,
  1273. .get_val = core_get_val,
  1274. };
  1275. static const struct pstate_funcs silvermont_funcs = {
  1276. .get_max = atom_get_max_pstate,
  1277. .get_max_physical = atom_get_max_pstate,
  1278. .get_min = atom_get_min_pstate,
  1279. .get_turbo = atom_get_turbo_pstate,
  1280. .get_val = atom_get_val,
  1281. .get_scaling = silvermont_get_scaling,
  1282. .get_vid = atom_get_vid,
  1283. };
  1284. static const struct pstate_funcs airmont_funcs = {
  1285. .get_max = atom_get_max_pstate,
  1286. .get_max_physical = atom_get_max_pstate,
  1287. .get_min = atom_get_min_pstate,
  1288. .get_turbo = atom_get_turbo_pstate,
  1289. .get_val = atom_get_val,
  1290. .get_scaling = airmont_get_scaling,
  1291. .get_vid = atom_get_vid,
  1292. };
  1293. static const struct pstate_funcs knl_funcs = {
  1294. .get_max = core_get_max_pstate,
  1295. .get_max_physical = core_get_max_pstate_physical,
  1296. .get_min = core_get_min_pstate,
  1297. .get_turbo = knl_get_turbo_pstate,
  1298. .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
  1299. .get_scaling = core_get_scaling,
  1300. .get_val = core_get_val,
  1301. };
  1302. #define ICPU(model, policy) \
  1303. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  1304. (unsigned long)&policy }
  1305. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  1306. ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
  1307. ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
  1308. ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_funcs),
  1309. ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
  1310. ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs),
  1311. ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs),
  1312. ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
  1313. ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
  1314. ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs),
  1315. ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs),
  1316. ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs),
  1317. ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
  1318. ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs),
  1319. ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
  1320. ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
  1321. ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
  1322. ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
  1323. ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
  1324. ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
  1325. ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, core_funcs),
  1326. ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
  1327. {}
  1328. };
  1329. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  1330. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
  1331. ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
  1332. ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
  1333. ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
  1334. {}
  1335. };
  1336. static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
  1337. ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
  1338. {}
  1339. };
  1340. static int intel_pstate_init_cpu(unsigned int cpunum)
  1341. {
  1342. struct cpudata *cpu;
  1343. cpu = all_cpu_data[cpunum];
  1344. if (!cpu) {
  1345. cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
  1346. if (!cpu)
  1347. return -ENOMEM;
  1348. all_cpu_data[cpunum] = cpu;
  1349. cpu->epp_default = -EINVAL;
  1350. cpu->epp_powersave = -EINVAL;
  1351. cpu->epp_saved = -EINVAL;
  1352. }
  1353. cpu = all_cpu_data[cpunum];
  1354. cpu->cpu = cpunum;
  1355. if (hwp_active) {
  1356. const struct x86_cpu_id *id;
  1357. id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
  1358. if (id)
  1359. intel_pstate_disable_ee(cpunum);
  1360. intel_pstate_hwp_enable(cpu);
  1361. }
  1362. intel_pstate_get_cpu_pstates(cpu);
  1363. pr_debug("controlling: cpu %d\n", cpunum);
  1364. return 0;
  1365. }
  1366. static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
  1367. {
  1368. struct cpudata *cpu = all_cpu_data[cpu_num];
  1369. if (hwp_active)
  1370. return;
  1371. if (cpu->update_util_set)
  1372. return;
  1373. /* Prevent intel_pstate_update_util() from using stale data. */
  1374. cpu->sample.time = 0;
  1375. cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
  1376. intel_pstate_update_util);
  1377. cpu->update_util_set = true;
  1378. }
  1379. static void intel_pstate_clear_update_util_hook(unsigned int cpu)
  1380. {
  1381. struct cpudata *cpu_data = all_cpu_data[cpu];
  1382. if (!cpu_data->update_util_set)
  1383. return;
  1384. cpufreq_remove_update_util_hook(cpu);
  1385. cpu_data->update_util_set = false;
  1386. synchronize_sched();
  1387. }
  1388. static int intel_pstate_get_max_freq(struct cpudata *cpu)
  1389. {
  1390. return global.turbo_disabled || global.no_turbo ?
  1391. cpu->pstate.max_freq : cpu->pstate.turbo_freq;
  1392. }
  1393. static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
  1394. struct cpudata *cpu)
  1395. {
  1396. int max_freq = intel_pstate_get_max_freq(cpu);
  1397. int32_t max_policy_perf, min_policy_perf;
  1398. int max_state, turbo_max;
  1399. /*
  1400. * HWP needs some special consideration, because on BDX the
  1401. * HWP_REQUEST uses abstract value to represent performance
  1402. * rather than pure ratios.
  1403. */
  1404. if (hwp_active) {
  1405. intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
  1406. } else {
  1407. max_state = intel_pstate_get_base_pstate(cpu);
  1408. turbo_max = cpu->pstate.turbo_pstate;
  1409. }
  1410. max_policy_perf = max_state * policy->max / max_freq;
  1411. if (policy->max == policy->min) {
  1412. min_policy_perf = max_policy_perf;
  1413. } else {
  1414. min_policy_perf = max_state * policy->min / max_freq;
  1415. min_policy_perf = clamp_t(int32_t, min_policy_perf,
  1416. 0, max_policy_perf);
  1417. }
  1418. pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
  1419. policy->cpu, max_state,
  1420. min_policy_perf, max_policy_perf);
  1421. /* Normalize user input to [min_perf, max_perf] */
  1422. if (per_cpu_limits) {
  1423. cpu->min_perf_ratio = min_policy_perf;
  1424. cpu->max_perf_ratio = max_policy_perf;
  1425. } else {
  1426. int32_t global_min, global_max;
  1427. /* Global limits are in percent of the maximum turbo P-state. */
  1428. global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
  1429. global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
  1430. global_min = clamp_t(int32_t, global_min, 0, global_max);
  1431. pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
  1432. global_min, global_max);
  1433. cpu->min_perf_ratio = max(min_policy_perf, global_min);
  1434. cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
  1435. cpu->max_perf_ratio = min(max_policy_perf, global_max);
  1436. cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
  1437. /* Make sure min_perf <= max_perf */
  1438. cpu->min_perf_ratio = min(cpu->min_perf_ratio,
  1439. cpu->max_perf_ratio);
  1440. }
  1441. pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
  1442. cpu->max_perf_ratio,
  1443. cpu->min_perf_ratio);
  1444. }
  1445. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  1446. {
  1447. struct cpudata *cpu;
  1448. if (!policy->cpuinfo.max_freq)
  1449. return -ENODEV;
  1450. pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
  1451. policy->cpuinfo.max_freq, policy->max);
  1452. cpu = all_cpu_data[policy->cpu];
  1453. cpu->policy = policy->policy;
  1454. mutex_lock(&intel_pstate_limits_lock);
  1455. intel_pstate_update_perf_limits(policy, cpu);
  1456. if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
  1457. /*
  1458. * NOHZ_FULL CPUs need this as the governor callback may not
  1459. * be invoked on them.
  1460. */
  1461. intel_pstate_clear_update_util_hook(policy->cpu);
  1462. intel_pstate_max_within_limits(cpu);
  1463. } else {
  1464. intel_pstate_set_update_util_hook(policy->cpu);
  1465. }
  1466. if (hwp_active)
  1467. intel_pstate_hwp_set(policy->cpu);
  1468. mutex_unlock(&intel_pstate_limits_lock);
  1469. return 0;
  1470. }
  1471. static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
  1472. struct cpudata *cpu)
  1473. {
  1474. if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
  1475. policy->max < policy->cpuinfo.max_freq &&
  1476. policy->max > cpu->pstate.max_freq) {
  1477. pr_debug("policy->max > max non turbo frequency\n");
  1478. policy->max = policy->cpuinfo.max_freq;
  1479. }
  1480. }
  1481. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  1482. {
  1483. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1484. update_turbo_state();
  1485. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  1486. intel_pstate_get_max_freq(cpu));
  1487. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  1488. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  1489. return -EINVAL;
  1490. intel_pstate_adjust_policy_max(policy, cpu);
  1491. return 0;
  1492. }
  1493. static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
  1494. {
  1495. intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
  1496. }
  1497. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  1498. {
  1499. pr_debug("CPU %d exiting\n", policy->cpu);
  1500. intel_pstate_clear_update_util_hook(policy->cpu);
  1501. if (hwp_active)
  1502. intel_pstate_hwp_save_state(policy);
  1503. else
  1504. intel_cpufreq_stop_cpu(policy);
  1505. }
  1506. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  1507. {
  1508. intel_pstate_exit_perf_limits(policy);
  1509. policy->fast_switch_possible = false;
  1510. return 0;
  1511. }
  1512. static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1513. {
  1514. struct cpudata *cpu;
  1515. int rc;
  1516. rc = intel_pstate_init_cpu(policy->cpu);
  1517. if (rc)
  1518. return rc;
  1519. cpu = all_cpu_data[policy->cpu];
  1520. cpu->max_perf_ratio = 0xFF;
  1521. cpu->min_perf_ratio = 0;
  1522. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1523. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1524. /* cpuinfo and default policy values */
  1525. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1526. update_turbo_state();
  1527. policy->cpuinfo.max_freq = global.turbo_disabled ?
  1528. cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
  1529. policy->cpuinfo.max_freq *= cpu->pstate.scaling;
  1530. intel_pstate_init_acpi_perf_limits(policy);
  1531. policy->fast_switch_possible = true;
  1532. return 0;
  1533. }
  1534. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1535. {
  1536. int ret = __intel_pstate_cpu_init(policy);
  1537. if (ret)
  1538. return ret;
  1539. if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
  1540. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1541. else
  1542. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1543. return 0;
  1544. }
  1545. static struct cpufreq_driver intel_pstate = {
  1546. .flags = CPUFREQ_CONST_LOOPS,
  1547. .verify = intel_pstate_verify_policy,
  1548. .setpolicy = intel_pstate_set_policy,
  1549. .suspend = intel_pstate_hwp_save_state,
  1550. .resume = intel_pstate_resume,
  1551. .init = intel_pstate_cpu_init,
  1552. .exit = intel_pstate_cpu_exit,
  1553. .stop_cpu = intel_pstate_stop_cpu,
  1554. .name = "intel_pstate",
  1555. };
  1556. static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
  1557. {
  1558. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1559. update_turbo_state();
  1560. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  1561. intel_pstate_get_max_freq(cpu));
  1562. intel_pstate_adjust_policy_max(policy, cpu);
  1563. intel_pstate_update_perf_limits(policy, cpu);
  1564. return 0;
  1565. }
  1566. static int intel_cpufreq_target(struct cpufreq_policy *policy,
  1567. unsigned int target_freq,
  1568. unsigned int relation)
  1569. {
  1570. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1571. struct cpufreq_freqs freqs;
  1572. int target_pstate;
  1573. update_turbo_state();
  1574. freqs.old = policy->cur;
  1575. freqs.new = target_freq;
  1576. cpufreq_freq_transition_begin(policy, &freqs);
  1577. switch (relation) {
  1578. case CPUFREQ_RELATION_L:
  1579. target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
  1580. break;
  1581. case CPUFREQ_RELATION_H:
  1582. target_pstate = freqs.new / cpu->pstate.scaling;
  1583. break;
  1584. default:
  1585. target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
  1586. break;
  1587. }
  1588. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  1589. if (target_pstate != cpu->pstate.current_pstate) {
  1590. cpu->pstate.current_pstate = target_pstate;
  1591. wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
  1592. pstate_funcs.get_val(cpu, target_pstate));
  1593. }
  1594. freqs.new = target_pstate * cpu->pstate.scaling;
  1595. cpufreq_freq_transition_end(policy, &freqs, false);
  1596. return 0;
  1597. }
  1598. static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
  1599. unsigned int target_freq)
  1600. {
  1601. struct cpudata *cpu = all_cpu_data[policy->cpu];
  1602. int target_pstate;
  1603. update_turbo_state();
  1604. target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
  1605. target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
  1606. intel_pstate_update_pstate(cpu, target_pstate);
  1607. return target_pstate * cpu->pstate.scaling;
  1608. }
  1609. static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
  1610. {
  1611. int ret = __intel_pstate_cpu_init(policy);
  1612. if (ret)
  1613. return ret;
  1614. policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
  1615. policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
  1616. /* This reflects the intel_pstate_get_cpu_pstates() setting. */
  1617. policy->cur = policy->cpuinfo.min_freq;
  1618. return 0;
  1619. }
  1620. static struct cpufreq_driver intel_cpufreq = {
  1621. .flags = CPUFREQ_CONST_LOOPS,
  1622. .verify = intel_cpufreq_verify_policy,
  1623. .target = intel_cpufreq_target,
  1624. .fast_switch = intel_cpufreq_fast_switch,
  1625. .init = intel_cpufreq_cpu_init,
  1626. .exit = intel_pstate_cpu_exit,
  1627. .stop_cpu = intel_cpufreq_stop_cpu,
  1628. .name = "intel_cpufreq",
  1629. };
  1630. static struct cpufreq_driver *default_driver = &intel_pstate;
  1631. static void intel_pstate_driver_cleanup(void)
  1632. {
  1633. unsigned int cpu;
  1634. get_online_cpus();
  1635. for_each_online_cpu(cpu) {
  1636. if (all_cpu_data[cpu]) {
  1637. if (intel_pstate_driver == &intel_pstate)
  1638. intel_pstate_clear_update_util_hook(cpu);
  1639. kfree(all_cpu_data[cpu]);
  1640. all_cpu_data[cpu] = NULL;
  1641. }
  1642. }
  1643. put_online_cpus();
  1644. intel_pstate_driver = NULL;
  1645. }
  1646. static int intel_pstate_register_driver(struct cpufreq_driver *driver)
  1647. {
  1648. int ret;
  1649. memset(&global, 0, sizeof(global));
  1650. global.max_perf_pct = 100;
  1651. intel_pstate_driver = driver;
  1652. ret = cpufreq_register_driver(intel_pstate_driver);
  1653. if (ret) {
  1654. intel_pstate_driver_cleanup();
  1655. return ret;
  1656. }
  1657. global.min_perf_pct = min_perf_pct_min();
  1658. return 0;
  1659. }
  1660. static int intel_pstate_unregister_driver(void)
  1661. {
  1662. if (hwp_active)
  1663. return -EBUSY;
  1664. cpufreq_unregister_driver(intel_pstate_driver);
  1665. intel_pstate_driver_cleanup();
  1666. return 0;
  1667. }
  1668. static ssize_t intel_pstate_show_status(char *buf)
  1669. {
  1670. if (!intel_pstate_driver)
  1671. return sprintf(buf, "off\n");
  1672. return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
  1673. "active" : "passive");
  1674. }
  1675. static int intel_pstate_update_status(const char *buf, size_t size)
  1676. {
  1677. int ret;
  1678. if (size == 3 && !strncmp(buf, "off", size))
  1679. return intel_pstate_driver ?
  1680. intel_pstate_unregister_driver() : -EINVAL;
  1681. if (size == 6 && !strncmp(buf, "active", size)) {
  1682. if (intel_pstate_driver) {
  1683. if (intel_pstate_driver == &intel_pstate)
  1684. return 0;
  1685. ret = intel_pstate_unregister_driver();
  1686. if (ret)
  1687. return ret;
  1688. }
  1689. return intel_pstate_register_driver(&intel_pstate);
  1690. }
  1691. if (size == 7 && !strncmp(buf, "passive", size)) {
  1692. if (intel_pstate_driver) {
  1693. if (intel_pstate_driver == &intel_cpufreq)
  1694. return 0;
  1695. ret = intel_pstate_unregister_driver();
  1696. if (ret)
  1697. return ret;
  1698. }
  1699. return intel_pstate_register_driver(&intel_cpufreq);
  1700. }
  1701. return -EINVAL;
  1702. }
  1703. static int no_load __initdata;
  1704. static int no_hwp __initdata;
  1705. static int hwp_only __initdata;
  1706. static unsigned int force_load __initdata;
  1707. static int __init intel_pstate_msrs_not_valid(void)
  1708. {
  1709. if (!pstate_funcs.get_max() ||
  1710. !pstate_funcs.get_min() ||
  1711. !pstate_funcs.get_turbo())
  1712. return -ENODEV;
  1713. return 0;
  1714. }
  1715. static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
  1716. {
  1717. pstate_funcs.get_max = funcs->get_max;
  1718. pstate_funcs.get_max_physical = funcs->get_max_physical;
  1719. pstate_funcs.get_min = funcs->get_min;
  1720. pstate_funcs.get_turbo = funcs->get_turbo;
  1721. pstate_funcs.get_scaling = funcs->get_scaling;
  1722. pstate_funcs.get_val = funcs->get_val;
  1723. pstate_funcs.get_vid = funcs->get_vid;
  1724. pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
  1725. }
  1726. #ifdef CONFIG_ACPI
  1727. static bool __init intel_pstate_no_acpi_pss(void)
  1728. {
  1729. int i;
  1730. for_each_possible_cpu(i) {
  1731. acpi_status status;
  1732. union acpi_object *pss;
  1733. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1734. struct acpi_processor *pr = per_cpu(processors, i);
  1735. if (!pr)
  1736. continue;
  1737. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  1738. if (ACPI_FAILURE(status))
  1739. continue;
  1740. pss = buffer.pointer;
  1741. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  1742. kfree(pss);
  1743. return false;
  1744. }
  1745. kfree(pss);
  1746. }
  1747. return true;
  1748. }
  1749. static bool __init intel_pstate_has_acpi_ppc(void)
  1750. {
  1751. int i;
  1752. for_each_possible_cpu(i) {
  1753. struct acpi_processor *pr = per_cpu(processors, i);
  1754. if (!pr)
  1755. continue;
  1756. if (acpi_has_method(pr->handle, "_PPC"))
  1757. return true;
  1758. }
  1759. return false;
  1760. }
  1761. enum {
  1762. PSS,
  1763. PPC,
  1764. };
  1765. /* Hardware vendor-specific info that has its own power management modes */
  1766. static struct acpi_platform_list plat_info[] __initdata = {
  1767. {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
  1768. {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1769. {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1770. {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1771. {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1772. {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1773. {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1774. {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1775. {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1776. {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1777. {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1778. {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1779. {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1780. {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1781. {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
  1782. { } /* End */
  1783. };
  1784. static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
  1785. {
  1786. const struct x86_cpu_id *id;
  1787. u64 misc_pwr;
  1788. int idx;
  1789. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  1790. if (id) {
  1791. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  1792. if ( misc_pwr & (1 << 8))
  1793. return true;
  1794. }
  1795. idx = acpi_match_platform_list(plat_info);
  1796. if (idx < 0)
  1797. return false;
  1798. switch (plat_info[idx].data) {
  1799. case PSS:
  1800. return intel_pstate_no_acpi_pss();
  1801. case PPC:
  1802. return intel_pstate_has_acpi_ppc() && !force_load;
  1803. }
  1804. return false;
  1805. }
  1806. static void intel_pstate_request_control_from_smm(void)
  1807. {
  1808. /*
  1809. * It may be unsafe to request P-states control from SMM if _PPC support
  1810. * has not been enabled.
  1811. */
  1812. if (acpi_ppc)
  1813. acpi_processor_pstate_control();
  1814. }
  1815. #else /* CONFIG_ACPI not enabled */
  1816. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  1817. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  1818. static inline void intel_pstate_request_control_from_smm(void) {}
  1819. #endif /* CONFIG_ACPI */
  1820. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  1821. { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
  1822. {}
  1823. };
  1824. static int __init intel_pstate_init(void)
  1825. {
  1826. int rc;
  1827. if (no_load)
  1828. return -ENODEV;
  1829. if (x86_match_cpu(hwp_support_ids)) {
  1830. copy_cpu_funcs(&core_funcs);
  1831. if (!no_hwp) {
  1832. hwp_active++;
  1833. intel_pstate.attr = hwp_cpufreq_attrs;
  1834. goto hwp_cpu_matched;
  1835. }
  1836. } else {
  1837. const struct x86_cpu_id *id;
  1838. id = x86_match_cpu(intel_pstate_cpu_ids);
  1839. if (!id)
  1840. return -ENODEV;
  1841. copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
  1842. }
  1843. if (intel_pstate_msrs_not_valid())
  1844. return -ENODEV;
  1845. hwp_cpu_matched:
  1846. /*
  1847. * The Intel pstate driver will be ignored if the platform
  1848. * firmware has its own power management modes.
  1849. */
  1850. if (intel_pstate_platform_pwr_mgmt_exists())
  1851. return -ENODEV;
  1852. if (!hwp_active && hwp_only)
  1853. return -ENOTSUPP;
  1854. pr_info("Intel P-state driver initializing\n");
  1855. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  1856. if (!all_cpu_data)
  1857. return -ENOMEM;
  1858. intel_pstate_request_control_from_smm();
  1859. intel_pstate_sysfs_expose_params();
  1860. mutex_lock(&intel_pstate_driver_lock);
  1861. rc = intel_pstate_register_driver(default_driver);
  1862. mutex_unlock(&intel_pstate_driver_lock);
  1863. if (rc)
  1864. return rc;
  1865. if (hwp_active)
  1866. pr_info("HWP enabled\n");
  1867. return 0;
  1868. }
  1869. device_initcall(intel_pstate_init);
  1870. static int __init intel_pstate_setup(char *str)
  1871. {
  1872. if (!str)
  1873. return -EINVAL;
  1874. if (!strcmp(str, "disable")) {
  1875. no_load = 1;
  1876. } else if (!strcmp(str, "passive")) {
  1877. pr_info("Passive mode enabled\n");
  1878. default_driver = &intel_cpufreq;
  1879. no_hwp = 1;
  1880. }
  1881. if (!strcmp(str, "no_hwp")) {
  1882. pr_info("HWP disabled\n");
  1883. no_hwp = 1;
  1884. }
  1885. if (!strcmp(str, "force"))
  1886. force_load = 1;
  1887. if (!strcmp(str, "hwp_only"))
  1888. hwp_only = 1;
  1889. if (!strcmp(str, "per_cpu_perf_limits"))
  1890. per_cpu_limits = true;
  1891. #ifdef CONFIG_ACPI
  1892. if (!strcmp(str, "support_acpi_ppc"))
  1893. acpi_ppc = true;
  1894. #endif
  1895. return 0;
  1896. }
  1897. early_param("intel_pstate", intel_pstate_setup);
  1898. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  1899. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  1900. MODULE_LICENSE("GPL");