intel_pstate.c 66 KB

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