intel_pstate.c 69 KB

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