intel_pstate.c 65 KB

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