intel_pstate.c 65 KB

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