intel_pstate.c 65 KB

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