intel_pstate.c 68 KB

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