intel_pstate.c 63 KB

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