intel_pstate.c 65 KB

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