intel_pstate.c 64 KB

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