intel_pstate.c 66 KB

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