intel_pstate.c 64 KB

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