intel_pstate.c 58 KB

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