intel_pstate.c 65 KB

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