intel_pstate.c 66 KB

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