intel_pstate.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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.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. #define ATOM_RATIOS 0x66a
  36. #define ATOM_VIDS 0x66b
  37. #define ATOM_TURBO_RATIOS 0x66c
  38. #define ATOM_TURBO_VIDS 0x66d
  39. #ifdef CONFIG_ACPI
  40. #include <acpi/processor.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. static inline int32_t mul_fp(int32_t x, int32_t y)
  48. {
  49. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  50. }
  51. static inline int32_t div_fp(s64 x, s64 y)
  52. {
  53. return div64_s64((int64_t)x << FRAC_BITS, y);
  54. }
  55. static inline int ceiling_fp(int32_t x)
  56. {
  57. int mask, ret;
  58. ret = fp_toint(x);
  59. mask = (1 << FRAC_BITS) - 1;
  60. if (x & mask)
  61. ret += 1;
  62. return ret;
  63. }
  64. static inline u64 mul_ext_fp(u64 x, u64 y)
  65. {
  66. return (x * y) >> EXT_FRAC_BITS;
  67. }
  68. static inline u64 div_ext_fp(u64 x, u64 y)
  69. {
  70. return div64_u64(x << EXT_FRAC_BITS, y);
  71. }
  72. /**
  73. * struct sample - Store performance sample
  74. * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
  75. * performance during last sample period
  76. * @busy_scaled: Scaled busy value which is used to calculate next
  77. * P state. This can be different than core_avg_perf
  78. * to account for cpu idle period
  79. * @aperf: Difference of actual performance frequency clock count
  80. * read from APERF MSR between last and current sample
  81. * @mperf: Difference of maximum performance frequency clock count
  82. * read from MPERF MSR between last and current sample
  83. * @tsc: Difference of time stamp counter between last and
  84. * current sample
  85. * @freq: Effective frequency calculated from APERF/MPERF
  86. * @time: Current time from scheduler
  87. *
  88. * This structure is used in the cpudata structure to store performance sample
  89. * data for choosing next P State.
  90. */
  91. struct sample {
  92. int32_t core_avg_perf;
  93. int32_t busy_scaled;
  94. u64 aperf;
  95. u64 mperf;
  96. u64 tsc;
  97. int freq;
  98. u64 time;
  99. };
  100. /**
  101. * struct pstate_data - Store P state data
  102. * @current_pstate: Current requested P state
  103. * @min_pstate: Min P state possible for this platform
  104. * @max_pstate: Max P state possible for this platform
  105. * @max_pstate_physical:This is physical Max P state for a processor
  106. * This can be higher than the max_pstate which can
  107. * be limited by platform thermal design power limits
  108. * @scaling: Scaling factor to convert frequency to cpufreq
  109. * frequency units
  110. * @turbo_pstate: Max Turbo P state possible for this platform
  111. *
  112. * Stores the per cpu model P state limits and current P state.
  113. */
  114. struct pstate_data {
  115. int current_pstate;
  116. int min_pstate;
  117. int max_pstate;
  118. int max_pstate_physical;
  119. int scaling;
  120. int turbo_pstate;
  121. };
  122. /**
  123. * struct vid_data - Stores voltage information data
  124. * @min: VID data for this platform corresponding to
  125. * the lowest P state
  126. * @max: VID data corresponding to the highest P State.
  127. * @turbo: VID data for turbo P state
  128. * @ratio: Ratio of (vid max - vid min) /
  129. * (max P state - Min P State)
  130. *
  131. * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
  132. * This data is used in Atom platforms, where in addition to target P state,
  133. * the voltage data needs to be specified to select next P State.
  134. */
  135. struct vid_data {
  136. int min;
  137. int max;
  138. int turbo;
  139. int32_t ratio;
  140. };
  141. /**
  142. * struct _pid - Stores PID data
  143. * @setpoint: Target set point for busyness or performance
  144. * @integral: Storage for accumulated error values
  145. * @p_gain: PID proportional gain
  146. * @i_gain: PID integral gain
  147. * @d_gain: PID derivative gain
  148. * @deadband: PID deadband
  149. * @last_err: Last error storage for integral part of PID calculation
  150. *
  151. * Stores PID coefficients and last error for PID controller.
  152. */
  153. struct _pid {
  154. int setpoint;
  155. int32_t integral;
  156. int32_t p_gain;
  157. int32_t i_gain;
  158. int32_t d_gain;
  159. int deadband;
  160. int32_t last_err;
  161. };
  162. /**
  163. * struct cpudata - Per CPU instance data storage
  164. * @cpu: CPU number for this instance data
  165. * @update_util: CPUFreq utility callback information
  166. * @update_util_set: CPUFreq utility callback is set
  167. * @pstate: Stores P state limits for this CPU
  168. * @vid: Stores VID limits for this CPU
  169. * @pid: Stores PID parameters for this CPU
  170. * @last_sample_time: Last Sample time
  171. * @prev_aperf: Last APERF value read from APERF MSR
  172. * @prev_mperf: Last MPERF value read from MPERF MSR
  173. * @prev_tsc: Last timestamp counter (TSC) value
  174. * @prev_cummulative_iowait: IO Wait time difference from last and
  175. * current sample
  176. * @sample: Storage for storing last Sample data
  177. * @acpi_perf_data: Stores ACPI perf information read from _PSS
  178. * @valid_pss_table: Set to true for valid ACPI _PSS entries found
  179. *
  180. * This structure stores per CPU instance data for all CPUs.
  181. */
  182. struct cpudata {
  183. int cpu;
  184. struct update_util_data update_util;
  185. bool update_util_set;
  186. struct pstate_data pstate;
  187. struct vid_data vid;
  188. struct _pid pid;
  189. u64 last_sample_time;
  190. u64 prev_aperf;
  191. u64 prev_mperf;
  192. u64 prev_tsc;
  193. u64 prev_cummulative_iowait;
  194. struct sample sample;
  195. #ifdef CONFIG_ACPI
  196. struct acpi_processor_performance acpi_perf_data;
  197. bool valid_pss_table;
  198. #endif
  199. };
  200. static struct cpudata **all_cpu_data;
  201. /**
  202. * struct pid_adjust_policy - Stores static PID configuration data
  203. * @sample_rate_ms: PID calculation sample rate in ms
  204. * @sample_rate_ns: Sample rate calculation in ns
  205. * @deadband: PID deadband
  206. * @setpoint: PID Setpoint
  207. * @p_gain_pct: PID proportional gain
  208. * @i_gain_pct: PID integral gain
  209. * @d_gain_pct: PID derivative gain
  210. *
  211. * Stores per CPU model static PID configuration data.
  212. */
  213. struct pstate_adjust_policy {
  214. int sample_rate_ms;
  215. s64 sample_rate_ns;
  216. int deadband;
  217. int setpoint;
  218. int p_gain_pct;
  219. int d_gain_pct;
  220. int i_gain_pct;
  221. };
  222. /**
  223. * struct pstate_funcs - Per CPU model specific callbacks
  224. * @get_max: Callback to get maximum non turbo effective P state
  225. * @get_max_physical: Callback to get maximum non turbo physical P state
  226. * @get_min: Callback to get minimum P state
  227. * @get_turbo: Callback to get turbo P state
  228. * @get_scaling: Callback to get frequency scaling factor
  229. * @get_val: Callback to convert P state to actual MSR write value
  230. * @get_vid: Callback to get VID data for Atom platforms
  231. * @get_target_pstate: Callback to a function to calculate next P state to use
  232. *
  233. * Core and Atom CPU models have different way to get P State limits. This
  234. * structure is used to store those callbacks.
  235. */
  236. struct pstate_funcs {
  237. int (*get_max)(void);
  238. int (*get_max_physical)(void);
  239. int (*get_min)(void);
  240. int (*get_turbo)(void);
  241. int (*get_scaling)(void);
  242. u64 (*get_val)(struct cpudata*, int pstate);
  243. void (*get_vid)(struct cpudata *);
  244. int32_t (*get_target_pstate)(struct cpudata *);
  245. };
  246. /**
  247. * struct cpu_defaults- Per CPU model default config data
  248. * @pid_policy: PID config data
  249. * @funcs: Callback function data
  250. */
  251. struct cpu_defaults {
  252. struct pstate_adjust_policy pid_policy;
  253. struct pstate_funcs funcs;
  254. };
  255. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
  256. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
  257. static struct pstate_adjust_policy pid_params;
  258. static struct pstate_funcs pstate_funcs;
  259. static int hwp_active;
  260. #ifdef CONFIG_ACPI
  261. static bool acpi_ppc;
  262. #endif
  263. /**
  264. * struct perf_limits - Store user and policy limits
  265. * @no_turbo: User requested turbo state from intel_pstate sysfs
  266. * @turbo_disabled: Platform turbo status either from msr
  267. * MSR_IA32_MISC_ENABLE or when maximum available pstate
  268. * matches the maximum turbo pstate
  269. * @max_perf_pct: Effective maximum performance limit in percentage, this
  270. * is minimum of either limits enforced by cpufreq policy
  271. * or limits from user set limits via intel_pstate sysfs
  272. * @min_perf_pct: Effective minimum performance limit in percentage, this
  273. * is maximum of either limits enforced by cpufreq policy
  274. * or limits from user set limits via intel_pstate sysfs
  275. * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
  276. * This value is used to limit max pstate
  277. * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
  278. * This value is used to limit min pstate
  279. * @max_policy_pct: The maximum performance in percentage enforced by
  280. * cpufreq setpolicy interface
  281. * @max_sysfs_pct: The maximum performance in percentage enforced by
  282. * intel pstate sysfs interface
  283. * @min_policy_pct: The minimum performance in percentage enforced by
  284. * cpufreq setpolicy interface
  285. * @min_sysfs_pct: The minimum performance in percentage enforced by
  286. * intel pstate sysfs interface
  287. *
  288. * Storage for user and policy defined limits.
  289. */
  290. struct perf_limits {
  291. int no_turbo;
  292. int turbo_disabled;
  293. int max_perf_pct;
  294. int min_perf_pct;
  295. int32_t max_perf;
  296. int32_t min_perf;
  297. int max_policy_pct;
  298. int max_sysfs_pct;
  299. int min_policy_pct;
  300. int min_sysfs_pct;
  301. };
  302. static struct perf_limits performance_limits = {
  303. .no_turbo = 0,
  304. .turbo_disabled = 0,
  305. .max_perf_pct = 100,
  306. .max_perf = int_tofp(1),
  307. .min_perf_pct = 100,
  308. .min_perf = int_tofp(1),
  309. .max_policy_pct = 100,
  310. .max_sysfs_pct = 100,
  311. .min_policy_pct = 0,
  312. .min_sysfs_pct = 0,
  313. };
  314. static struct perf_limits powersave_limits = {
  315. .no_turbo = 0,
  316. .turbo_disabled = 0,
  317. .max_perf_pct = 100,
  318. .max_perf = int_tofp(1),
  319. .min_perf_pct = 0,
  320. .min_perf = 0,
  321. .max_policy_pct = 100,
  322. .max_sysfs_pct = 100,
  323. .min_policy_pct = 0,
  324. .min_sysfs_pct = 0,
  325. };
  326. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  327. static struct perf_limits *limits = &performance_limits;
  328. #else
  329. static struct perf_limits *limits = &powersave_limits;
  330. #endif
  331. #ifdef CONFIG_ACPI
  332. static bool intel_pstate_get_ppc_enable_status(void)
  333. {
  334. if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
  335. acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
  336. return true;
  337. return acpi_ppc;
  338. }
  339. /*
  340. * The max target pstate ratio is a 8 bit value in both PLATFORM_INFO MSR and
  341. * in TURBO_RATIO_LIMIT MSR, which pstate driver stores in max_pstate and
  342. * max_turbo_pstate fields. The PERF_CTL MSR contains 16 bit value for P state
  343. * ratio, out of it only high 8 bits are used. For example 0x1700 is setting
  344. * target ratio 0x17. The _PSS control value stores in a format which can be
  345. * directly written to PERF_CTL MSR. But in intel_pstate driver this shift
  346. * occurs during write to PERF_CTL (E.g. for cores core_set_pstate()).
  347. * This function converts the _PSS control value to intel pstate driver format
  348. * for comparison and assignment.
  349. */
  350. static int convert_to_native_pstate_format(struct cpudata *cpu, int index)
  351. {
  352. return cpu->acpi_perf_data.states[index].control >> 8;
  353. }
  354. static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  355. {
  356. struct cpudata *cpu;
  357. int turbo_pss_ctl;
  358. int ret;
  359. int i;
  360. if (hwp_active)
  361. return;
  362. if (!intel_pstate_get_ppc_enable_status())
  363. return;
  364. cpu = all_cpu_data[policy->cpu];
  365. ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
  366. policy->cpu);
  367. if (ret)
  368. return;
  369. /*
  370. * Check if the control value in _PSS is for PERF_CTL MSR, which should
  371. * guarantee that the states returned by it map to the states in our
  372. * list directly.
  373. */
  374. if (cpu->acpi_perf_data.control_register.space_id !=
  375. ACPI_ADR_SPACE_FIXED_HARDWARE)
  376. goto err;
  377. /*
  378. * If there is only one entry _PSS, simply ignore _PSS and continue as
  379. * usual without taking _PSS into account
  380. */
  381. if (cpu->acpi_perf_data.state_count < 2)
  382. goto err;
  383. pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
  384. for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
  385. pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
  386. (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
  387. (u32) cpu->acpi_perf_data.states[i].core_frequency,
  388. (u32) cpu->acpi_perf_data.states[i].power,
  389. (u32) cpu->acpi_perf_data.states[i].control);
  390. }
  391. /*
  392. * The _PSS table doesn't contain whole turbo frequency range.
  393. * This just contains +1 MHZ above the max non turbo frequency,
  394. * with control value corresponding to max turbo ratio. But
  395. * when cpufreq set policy is called, it will call with this
  396. * max frequency, which will cause a reduced performance as
  397. * this driver uses real max turbo frequency as the max
  398. * frequency. So correct this frequency in _PSS table to
  399. * correct max turbo frequency based on the turbo ratio.
  400. * Also need to convert to MHz as _PSS freq is in MHz.
  401. */
  402. turbo_pss_ctl = convert_to_native_pstate_format(cpu, 0);
  403. if (turbo_pss_ctl > cpu->pstate.max_pstate)
  404. cpu->acpi_perf_data.states[0].core_frequency =
  405. policy->cpuinfo.max_freq / 1000;
  406. cpu->valid_pss_table = true;
  407. pr_debug("_PPC limits will be enforced\n");
  408. return;
  409. err:
  410. cpu->valid_pss_table = false;
  411. acpi_processor_unregister_performance(policy->cpu);
  412. }
  413. static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  414. {
  415. struct cpudata *cpu;
  416. cpu = all_cpu_data[policy->cpu];
  417. if (!cpu->valid_pss_table)
  418. return;
  419. acpi_processor_unregister_performance(policy->cpu);
  420. }
  421. #else
  422. static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
  423. {
  424. }
  425. static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  426. {
  427. }
  428. #endif
  429. static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
  430. int deadband, int integral) {
  431. pid->setpoint = int_tofp(setpoint);
  432. pid->deadband = int_tofp(deadband);
  433. pid->integral = int_tofp(integral);
  434. pid->last_err = int_tofp(setpoint) - int_tofp(busy);
  435. }
  436. static inline void pid_p_gain_set(struct _pid *pid, int percent)
  437. {
  438. pid->p_gain = div_fp(percent, 100);
  439. }
  440. static inline void pid_i_gain_set(struct _pid *pid, int percent)
  441. {
  442. pid->i_gain = div_fp(percent, 100);
  443. }
  444. static inline void pid_d_gain_set(struct _pid *pid, int percent)
  445. {
  446. pid->d_gain = div_fp(percent, 100);
  447. }
  448. static signed int pid_calc(struct _pid *pid, int32_t busy)
  449. {
  450. signed int result;
  451. int32_t pterm, dterm, fp_error;
  452. int32_t integral_limit;
  453. fp_error = pid->setpoint - busy;
  454. if (abs(fp_error) <= pid->deadband)
  455. return 0;
  456. pterm = mul_fp(pid->p_gain, fp_error);
  457. pid->integral += fp_error;
  458. /*
  459. * We limit the integral here so that it will never
  460. * get higher than 30. This prevents it from becoming
  461. * too large an input over long periods of time and allows
  462. * it to get factored out sooner.
  463. *
  464. * The value of 30 was chosen through experimentation.
  465. */
  466. integral_limit = int_tofp(30);
  467. if (pid->integral > integral_limit)
  468. pid->integral = integral_limit;
  469. if (pid->integral < -integral_limit)
  470. pid->integral = -integral_limit;
  471. dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
  472. pid->last_err = fp_error;
  473. result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
  474. result = result + (1 << (FRAC_BITS-1));
  475. return (signed int)fp_toint(result);
  476. }
  477. static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
  478. {
  479. pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
  480. pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
  481. pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
  482. pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
  483. }
  484. static inline void intel_pstate_reset_all_pid(void)
  485. {
  486. unsigned int cpu;
  487. for_each_online_cpu(cpu) {
  488. if (all_cpu_data[cpu])
  489. intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
  490. }
  491. }
  492. static inline void update_turbo_state(void)
  493. {
  494. u64 misc_en;
  495. struct cpudata *cpu;
  496. cpu = all_cpu_data[0];
  497. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  498. limits->turbo_disabled =
  499. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  500. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  501. }
  502. static void intel_pstate_hwp_set(const struct cpumask *cpumask)
  503. {
  504. int min, hw_min, max, hw_max, cpu, range, adj_range;
  505. u64 value, cap;
  506. rdmsrl(MSR_HWP_CAPABILITIES, cap);
  507. hw_min = HWP_LOWEST_PERF(cap);
  508. hw_max = HWP_HIGHEST_PERF(cap);
  509. range = hw_max - hw_min;
  510. for_each_cpu(cpu, cpumask) {
  511. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  512. adj_range = limits->min_perf_pct * range / 100;
  513. min = hw_min + adj_range;
  514. value &= ~HWP_MIN_PERF(~0L);
  515. value |= HWP_MIN_PERF(min);
  516. adj_range = limits->max_perf_pct * range / 100;
  517. max = hw_min + adj_range;
  518. if (limits->no_turbo) {
  519. hw_max = HWP_GUARANTEED_PERF(cap);
  520. if (hw_max < max)
  521. max = hw_max;
  522. }
  523. value &= ~HWP_MAX_PERF(~0L);
  524. value |= HWP_MAX_PERF(max);
  525. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  526. }
  527. }
  528. static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
  529. {
  530. if (hwp_active)
  531. intel_pstate_hwp_set(policy->cpus);
  532. return 0;
  533. }
  534. static void intel_pstate_hwp_set_online_cpus(void)
  535. {
  536. get_online_cpus();
  537. intel_pstate_hwp_set(cpu_online_mask);
  538. put_online_cpus();
  539. }
  540. /************************** debugfs begin ************************/
  541. static int pid_param_set(void *data, u64 val)
  542. {
  543. *(u32 *)data = val;
  544. intel_pstate_reset_all_pid();
  545. return 0;
  546. }
  547. static int pid_param_get(void *data, u64 *val)
  548. {
  549. *val = *(u32 *)data;
  550. return 0;
  551. }
  552. DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
  553. struct pid_param {
  554. char *name;
  555. void *value;
  556. };
  557. static struct pid_param pid_files[] = {
  558. {"sample_rate_ms", &pid_params.sample_rate_ms},
  559. {"d_gain_pct", &pid_params.d_gain_pct},
  560. {"i_gain_pct", &pid_params.i_gain_pct},
  561. {"deadband", &pid_params.deadband},
  562. {"setpoint", &pid_params.setpoint},
  563. {"p_gain_pct", &pid_params.p_gain_pct},
  564. {NULL, NULL}
  565. };
  566. static void __init intel_pstate_debug_expose_params(void)
  567. {
  568. struct dentry *debugfs_parent;
  569. int i = 0;
  570. if (hwp_active)
  571. return;
  572. debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
  573. if (IS_ERR_OR_NULL(debugfs_parent))
  574. return;
  575. while (pid_files[i].name) {
  576. debugfs_create_file(pid_files[i].name, 0660,
  577. debugfs_parent, pid_files[i].value,
  578. &fops_pid_param);
  579. i++;
  580. }
  581. }
  582. /************************** debugfs end ************************/
  583. /************************** sysfs begin ************************/
  584. #define show_one(file_name, object) \
  585. static ssize_t show_##file_name \
  586. (struct kobject *kobj, struct attribute *attr, char *buf) \
  587. { \
  588. return sprintf(buf, "%u\n", limits->object); \
  589. }
  590. static ssize_t show_turbo_pct(struct kobject *kobj,
  591. struct attribute *attr, char *buf)
  592. {
  593. struct cpudata *cpu;
  594. int total, no_turbo, turbo_pct;
  595. uint32_t turbo_fp;
  596. cpu = all_cpu_data[0];
  597. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  598. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  599. turbo_fp = div_fp(no_turbo, total);
  600. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  601. return sprintf(buf, "%u\n", turbo_pct);
  602. }
  603. static ssize_t show_num_pstates(struct kobject *kobj,
  604. struct attribute *attr, char *buf)
  605. {
  606. struct cpudata *cpu;
  607. int total;
  608. cpu = all_cpu_data[0];
  609. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  610. return sprintf(buf, "%u\n", total);
  611. }
  612. static ssize_t show_no_turbo(struct kobject *kobj,
  613. struct attribute *attr, char *buf)
  614. {
  615. ssize_t ret;
  616. update_turbo_state();
  617. if (limits->turbo_disabled)
  618. ret = sprintf(buf, "%u\n", limits->turbo_disabled);
  619. else
  620. ret = sprintf(buf, "%u\n", limits->no_turbo);
  621. return ret;
  622. }
  623. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  624. const char *buf, size_t count)
  625. {
  626. unsigned int input;
  627. int ret;
  628. ret = sscanf(buf, "%u", &input);
  629. if (ret != 1)
  630. return -EINVAL;
  631. update_turbo_state();
  632. if (limits->turbo_disabled) {
  633. pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
  634. return -EPERM;
  635. }
  636. limits->no_turbo = clamp_t(int, input, 0, 1);
  637. if (hwp_active)
  638. intel_pstate_hwp_set_online_cpus();
  639. return count;
  640. }
  641. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  642. const char *buf, size_t count)
  643. {
  644. unsigned int input;
  645. int ret;
  646. ret = sscanf(buf, "%u", &input);
  647. if (ret != 1)
  648. return -EINVAL;
  649. limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
  650. limits->max_perf_pct = min(limits->max_policy_pct,
  651. limits->max_sysfs_pct);
  652. limits->max_perf_pct = max(limits->min_policy_pct,
  653. limits->max_perf_pct);
  654. limits->max_perf_pct = max(limits->min_perf_pct,
  655. limits->max_perf_pct);
  656. limits->max_perf = div_fp(limits->max_perf_pct, 100);
  657. if (hwp_active)
  658. intel_pstate_hwp_set_online_cpus();
  659. return count;
  660. }
  661. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  662. const char *buf, size_t count)
  663. {
  664. unsigned int input;
  665. int ret;
  666. ret = sscanf(buf, "%u", &input);
  667. if (ret != 1)
  668. return -EINVAL;
  669. limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
  670. limits->min_perf_pct = max(limits->min_policy_pct,
  671. limits->min_sysfs_pct);
  672. limits->min_perf_pct = min(limits->max_policy_pct,
  673. limits->min_perf_pct);
  674. limits->min_perf_pct = min(limits->max_perf_pct,
  675. limits->min_perf_pct);
  676. limits->min_perf = div_fp(limits->min_perf_pct, 100);
  677. if (hwp_active)
  678. intel_pstate_hwp_set_online_cpus();
  679. return count;
  680. }
  681. show_one(max_perf_pct, max_perf_pct);
  682. show_one(min_perf_pct, min_perf_pct);
  683. define_one_global_rw(no_turbo);
  684. define_one_global_rw(max_perf_pct);
  685. define_one_global_rw(min_perf_pct);
  686. define_one_global_ro(turbo_pct);
  687. define_one_global_ro(num_pstates);
  688. static struct attribute *intel_pstate_attributes[] = {
  689. &no_turbo.attr,
  690. &max_perf_pct.attr,
  691. &min_perf_pct.attr,
  692. &turbo_pct.attr,
  693. &num_pstates.attr,
  694. NULL
  695. };
  696. static struct attribute_group intel_pstate_attr_group = {
  697. .attrs = intel_pstate_attributes,
  698. };
  699. static void __init intel_pstate_sysfs_expose_params(void)
  700. {
  701. struct kobject *intel_pstate_kobject;
  702. int rc;
  703. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  704. &cpu_subsys.dev_root->kobj);
  705. BUG_ON(!intel_pstate_kobject);
  706. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  707. BUG_ON(rc);
  708. }
  709. /************************** sysfs end ************************/
  710. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  711. {
  712. /* First disable HWP notification interrupt as we don't process them */
  713. wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
  714. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  715. }
  716. static int atom_get_min_pstate(void)
  717. {
  718. u64 value;
  719. rdmsrl(ATOM_RATIOS, value);
  720. return (value >> 8) & 0x7F;
  721. }
  722. static int atom_get_max_pstate(void)
  723. {
  724. u64 value;
  725. rdmsrl(ATOM_RATIOS, value);
  726. return (value >> 16) & 0x7F;
  727. }
  728. static int atom_get_turbo_pstate(void)
  729. {
  730. u64 value;
  731. rdmsrl(ATOM_TURBO_RATIOS, value);
  732. return value & 0x7F;
  733. }
  734. static u64 atom_get_val(struct cpudata *cpudata, int pstate)
  735. {
  736. u64 val;
  737. int32_t vid_fp;
  738. u32 vid;
  739. val = (u64)pstate << 8;
  740. if (limits->no_turbo && !limits->turbo_disabled)
  741. val |= (u64)1 << 32;
  742. vid_fp = cpudata->vid.min + mul_fp(
  743. int_tofp(pstate - cpudata->pstate.min_pstate),
  744. cpudata->vid.ratio);
  745. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  746. vid = ceiling_fp(vid_fp);
  747. if (pstate > cpudata->pstate.max_pstate)
  748. vid = cpudata->vid.turbo;
  749. return val | vid;
  750. }
  751. static int silvermont_get_scaling(void)
  752. {
  753. u64 value;
  754. int i;
  755. /* Defined in Table 35-6 from SDM (Sept 2015) */
  756. static int silvermont_freq_table[] = {
  757. 83300, 100000, 133300, 116700, 80000};
  758. rdmsrl(MSR_FSB_FREQ, value);
  759. i = value & 0x7;
  760. WARN_ON(i > 4);
  761. return silvermont_freq_table[i];
  762. }
  763. static int airmont_get_scaling(void)
  764. {
  765. u64 value;
  766. int i;
  767. /* Defined in Table 35-10 from SDM (Sept 2015) */
  768. static int airmont_freq_table[] = {
  769. 83300, 100000, 133300, 116700, 80000,
  770. 93300, 90000, 88900, 87500};
  771. rdmsrl(MSR_FSB_FREQ, value);
  772. i = value & 0xF;
  773. WARN_ON(i > 8);
  774. return airmont_freq_table[i];
  775. }
  776. static void atom_get_vid(struct cpudata *cpudata)
  777. {
  778. u64 value;
  779. rdmsrl(ATOM_VIDS, value);
  780. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  781. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  782. cpudata->vid.ratio = div_fp(
  783. cpudata->vid.max - cpudata->vid.min,
  784. int_tofp(cpudata->pstate.max_pstate -
  785. cpudata->pstate.min_pstate));
  786. rdmsrl(ATOM_TURBO_VIDS, value);
  787. cpudata->vid.turbo = value & 0x7f;
  788. }
  789. static int core_get_min_pstate(void)
  790. {
  791. u64 value;
  792. rdmsrl(MSR_PLATFORM_INFO, value);
  793. return (value >> 40) & 0xFF;
  794. }
  795. static int core_get_max_pstate_physical(void)
  796. {
  797. u64 value;
  798. rdmsrl(MSR_PLATFORM_INFO, value);
  799. return (value >> 8) & 0xFF;
  800. }
  801. static int core_get_max_pstate(void)
  802. {
  803. u64 tar;
  804. u64 plat_info;
  805. int max_pstate;
  806. int err;
  807. rdmsrl(MSR_PLATFORM_INFO, plat_info);
  808. max_pstate = (plat_info >> 8) & 0xFF;
  809. err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
  810. if (!err) {
  811. /* Do some sanity checking for safety */
  812. if (plat_info & 0x600000000) {
  813. u64 tdp_ctrl;
  814. u64 tdp_ratio;
  815. int tdp_msr;
  816. err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  817. if (err)
  818. goto skip_tar;
  819. tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
  820. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  821. if (err)
  822. goto skip_tar;
  823. /* For level 1 and 2, bits[23:16] contain the ratio */
  824. if (tdp_ctrl)
  825. tdp_ratio >>= 16;
  826. tdp_ratio &= 0xff; /* ratios are only 8 bits long */
  827. if (tdp_ratio - 1 == tar) {
  828. max_pstate = tar;
  829. pr_debug("max_pstate=TAC %x\n", max_pstate);
  830. } else {
  831. goto skip_tar;
  832. }
  833. }
  834. }
  835. skip_tar:
  836. return max_pstate;
  837. }
  838. static int core_get_turbo_pstate(void)
  839. {
  840. u64 value;
  841. int nont, ret;
  842. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  843. nont = core_get_max_pstate();
  844. ret = (value) & 255;
  845. if (ret <= nont)
  846. ret = nont;
  847. return ret;
  848. }
  849. static inline int core_get_scaling(void)
  850. {
  851. return 100000;
  852. }
  853. static u64 core_get_val(struct cpudata *cpudata, int pstate)
  854. {
  855. u64 val;
  856. val = (u64)pstate << 8;
  857. if (limits->no_turbo && !limits->turbo_disabled)
  858. val |= (u64)1 << 32;
  859. return val;
  860. }
  861. static int knl_get_turbo_pstate(void)
  862. {
  863. u64 value;
  864. int nont, ret;
  865. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  866. nont = core_get_max_pstate();
  867. ret = (((value) >> 8) & 0xFF);
  868. if (ret <= nont)
  869. ret = nont;
  870. return ret;
  871. }
  872. static struct cpu_defaults core_params = {
  873. .pid_policy = {
  874. .sample_rate_ms = 10,
  875. .deadband = 0,
  876. .setpoint = 97,
  877. .p_gain_pct = 20,
  878. .d_gain_pct = 0,
  879. .i_gain_pct = 0,
  880. },
  881. .funcs = {
  882. .get_max = core_get_max_pstate,
  883. .get_max_physical = core_get_max_pstate_physical,
  884. .get_min = core_get_min_pstate,
  885. .get_turbo = core_get_turbo_pstate,
  886. .get_scaling = core_get_scaling,
  887. .get_val = core_get_val,
  888. .get_target_pstate = get_target_pstate_use_performance,
  889. },
  890. };
  891. static struct cpu_defaults silvermont_params = {
  892. .pid_policy = {
  893. .sample_rate_ms = 10,
  894. .deadband = 0,
  895. .setpoint = 60,
  896. .p_gain_pct = 14,
  897. .d_gain_pct = 0,
  898. .i_gain_pct = 4,
  899. },
  900. .funcs = {
  901. .get_max = atom_get_max_pstate,
  902. .get_max_physical = atom_get_max_pstate,
  903. .get_min = atom_get_min_pstate,
  904. .get_turbo = atom_get_turbo_pstate,
  905. .get_val = atom_get_val,
  906. .get_scaling = silvermont_get_scaling,
  907. .get_vid = atom_get_vid,
  908. .get_target_pstate = get_target_pstate_use_cpu_load,
  909. },
  910. };
  911. static struct cpu_defaults airmont_params = {
  912. .pid_policy = {
  913. .sample_rate_ms = 10,
  914. .deadband = 0,
  915. .setpoint = 60,
  916. .p_gain_pct = 14,
  917. .d_gain_pct = 0,
  918. .i_gain_pct = 4,
  919. },
  920. .funcs = {
  921. .get_max = atom_get_max_pstate,
  922. .get_max_physical = atom_get_max_pstate,
  923. .get_min = atom_get_min_pstate,
  924. .get_turbo = atom_get_turbo_pstate,
  925. .get_val = atom_get_val,
  926. .get_scaling = airmont_get_scaling,
  927. .get_vid = atom_get_vid,
  928. .get_target_pstate = get_target_pstate_use_cpu_load,
  929. },
  930. };
  931. static struct cpu_defaults knl_params = {
  932. .pid_policy = {
  933. .sample_rate_ms = 10,
  934. .deadband = 0,
  935. .setpoint = 97,
  936. .p_gain_pct = 20,
  937. .d_gain_pct = 0,
  938. .i_gain_pct = 0,
  939. },
  940. .funcs = {
  941. .get_max = core_get_max_pstate,
  942. .get_max_physical = core_get_max_pstate_physical,
  943. .get_min = core_get_min_pstate,
  944. .get_turbo = knl_get_turbo_pstate,
  945. .get_scaling = core_get_scaling,
  946. .get_val = core_get_val,
  947. .get_target_pstate = get_target_pstate_use_performance,
  948. },
  949. };
  950. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  951. {
  952. int max_perf = cpu->pstate.turbo_pstate;
  953. int max_perf_adj;
  954. int min_perf;
  955. if (limits->no_turbo || limits->turbo_disabled)
  956. max_perf = cpu->pstate.max_pstate;
  957. /*
  958. * performance can be limited by user through sysfs, by cpufreq
  959. * policy, or by cpu specific default values determined through
  960. * experimentation.
  961. */
  962. max_perf_adj = fp_toint(max_perf * limits->max_perf);
  963. *max = clamp_t(int, max_perf_adj,
  964. cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
  965. min_perf = fp_toint(max_perf * limits->min_perf);
  966. *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
  967. }
  968. static inline void intel_pstate_record_pstate(struct cpudata *cpu, int pstate)
  969. {
  970. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  971. cpu->pstate.current_pstate = pstate;
  972. }
  973. static void intel_pstate_set_min_pstate(struct cpudata *cpu)
  974. {
  975. int pstate = cpu->pstate.min_pstate;
  976. intel_pstate_record_pstate(cpu, pstate);
  977. /*
  978. * Generally, there is no guarantee that this code will always run on
  979. * the CPU being updated, so force the register update to run on the
  980. * right CPU.
  981. */
  982. wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
  983. pstate_funcs.get_val(cpu, pstate));
  984. }
  985. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  986. {
  987. cpu->pstate.min_pstate = pstate_funcs.get_min();
  988. cpu->pstate.max_pstate = pstate_funcs.get_max();
  989. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  990. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  991. cpu->pstate.scaling = pstate_funcs.get_scaling();
  992. if (pstate_funcs.get_vid)
  993. pstate_funcs.get_vid(cpu);
  994. intel_pstate_set_min_pstate(cpu);
  995. }
  996. static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
  997. {
  998. struct sample *sample = &cpu->sample;
  999. sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
  1000. }
  1001. static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
  1002. {
  1003. u64 aperf, mperf;
  1004. unsigned long flags;
  1005. u64 tsc;
  1006. local_irq_save(flags);
  1007. rdmsrl(MSR_IA32_APERF, aperf);
  1008. rdmsrl(MSR_IA32_MPERF, mperf);
  1009. tsc = rdtsc();
  1010. if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
  1011. local_irq_restore(flags);
  1012. return false;
  1013. }
  1014. local_irq_restore(flags);
  1015. cpu->last_sample_time = cpu->sample.time;
  1016. cpu->sample.time = time;
  1017. cpu->sample.aperf = aperf;
  1018. cpu->sample.mperf = mperf;
  1019. cpu->sample.tsc = tsc;
  1020. cpu->sample.aperf -= cpu->prev_aperf;
  1021. cpu->sample.mperf -= cpu->prev_mperf;
  1022. cpu->sample.tsc -= cpu->prev_tsc;
  1023. cpu->prev_aperf = aperf;
  1024. cpu->prev_mperf = mperf;
  1025. cpu->prev_tsc = tsc;
  1026. /*
  1027. * First time this function is invoked in a given cycle, all of the
  1028. * previous sample data fields are equal to zero or stale and they must
  1029. * be populated with meaningful numbers for things to work, so assume
  1030. * that sample.time will always be reset before setting the utilization
  1031. * update hook and make the caller skip the sample then.
  1032. */
  1033. return !!cpu->last_sample_time;
  1034. }
  1035. static inline int32_t get_avg_frequency(struct cpudata *cpu)
  1036. {
  1037. return mul_ext_fp(cpu->sample.core_avg_perf,
  1038. cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
  1039. }
  1040. static inline int32_t get_avg_pstate(struct cpudata *cpu)
  1041. {
  1042. return mul_ext_fp(cpu->pstate.max_pstate_physical,
  1043. cpu->sample.core_avg_perf);
  1044. }
  1045. static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
  1046. {
  1047. struct sample *sample = &cpu->sample;
  1048. u64 cummulative_iowait, delta_iowait_us;
  1049. u64 delta_iowait_mperf;
  1050. u64 mperf, now;
  1051. int32_t cpu_load;
  1052. cummulative_iowait = get_cpu_iowait_time_us(cpu->cpu, &now);
  1053. /*
  1054. * Convert iowait time into number of IO cycles spent at max_freq.
  1055. * IO is considered as busy only for the cpu_load algorithm. For
  1056. * performance this is not needed since we always try to reach the
  1057. * maximum P-State, so we are already boosting the IOs.
  1058. */
  1059. delta_iowait_us = cummulative_iowait - cpu->prev_cummulative_iowait;
  1060. delta_iowait_mperf = div64_u64(delta_iowait_us * cpu->pstate.scaling *
  1061. cpu->pstate.max_pstate, MSEC_PER_SEC);
  1062. mperf = cpu->sample.mperf + delta_iowait_mperf;
  1063. cpu->prev_cummulative_iowait = cummulative_iowait;
  1064. /*
  1065. * The load can be estimated as the ratio of the mperf counter
  1066. * running at a constant frequency during active periods
  1067. * (C0) and the time stamp counter running at the same frequency
  1068. * also during C-states.
  1069. */
  1070. cpu_load = div64_u64(int_tofp(100) * mperf, sample->tsc);
  1071. cpu->sample.busy_scaled = cpu_load;
  1072. return get_avg_pstate(cpu) - pid_calc(&cpu->pid, cpu_load);
  1073. }
  1074. static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
  1075. {
  1076. int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
  1077. u64 duration_ns;
  1078. /*
  1079. * perf_scaled is the average performance during the last sampling
  1080. * period scaled by the ratio of the maximum P-state to the P-state
  1081. * requested last time (in percent). That measures the system's
  1082. * response to the previous P-state selection.
  1083. */
  1084. max_pstate = cpu->pstate.max_pstate_physical;
  1085. current_pstate = cpu->pstate.current_pstate;
  1086. perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
  1087. div_fp(100 * max_pstate, current_pstate));
  1088. /*
  1089. * Since our utilization update callback will not run unless we are
  1090. * in C0, check if the actual elapsed time is significantly greater (3x)
  1091. * than our sample interval. If it is, then we were idle for a long
  1092. * enough period of time to adjust our performance metric.
  1093. */
  1094. duration_ns = cpu->sample.time - cpu->last_sample_time;
  1095. if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
  1096. sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
  1097. perf_scaled = mul_fp(perf_scaled, sample_ratio);
  1098. } else {
  1099. sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
  1100. if (sample_ratio < int_tofp(1))
  1101. perf_scaled = 0;
  1102. }
  1103. cpu->sample.busy_scaled = perf_scaled;
  1104. return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
  1105. }
  1106. static inline void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
  1107. {
  1108. int max_perf, min_perf;
  1109. update_turbo_state();
  1110. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  1111. pstate = clamp_t(int, pstate, min_perf, max_perf);
  1112. if (pstate == cpu->pstate.current_pstate)
  1113. return;
  1114. intel_pstate_record_pstate(cpu, pstate);
  1115. wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
  1116. }
  1117. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  1118. {
  1119. int from, target_pstate;
  1120. struct sample *sample;
  1121. from = cpu->pstate.current_pstate;
  1122. target_pstate = pstate_funcs.get_target_pstate(cpu);
  1123. intel_pstate_update_pstate(cpu, target_pstate);
  1124. sample = &cpu->sample;
  1125. trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
  1126. fp_toint(sample->busy_scaled),
  1127. from,
  1128. cpu->pstate.current_pstate,
  1129. sample->mperf,
  1130. sample->aperf,
  1131. sample->tsc,
  1132. get_avg_frequency(cpu));
  1133. }
  1134. static void intel_pstate_update_util(struct update_util_data *data, u64 time,
  1135. unsigned long util, unsigned long max)
  1136. {
  1137. struct cpudata *cpu = container_of(data, struct cpudata, update_util);
  1138. u64 delta_ns = time - cpu->sample.time;
  1139. if ((s64)delta_ns >= pid_params.sample_rate_ns) {
  1140. bool sample_taken = intel_pstate_sample(cpu, time);
  1141. if (sample_taken) {
  1142. intel_pstate_calc_avg_perf(cpu);
  1143. if (!hwp_active)
  1144. intel_pstate_adjust_busy_pstate(cpu);
  1145. }
  1146. }
  1147. }
  1148. #define ICPU(model, policy) \
  1149. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  1150. (unsigned long)&policy }
  1151. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  1152. ICPU(0x2a, core_params),
  1153. ICPU(0x2d, core_params),
  1154. ICPU(0x37, silvermont_params),
  1155. ICPU(0x3a, core_params),
  1156. ICPU(0x3c, core_params),
  1157. ICPU(0x3d, core_params),
  1158. ICPU(0x3e, core_params),
  1159. ICPU(0x3f, core_params),
  1160. ICPU(0x45, core_params),
  1161. ICPU(0x46, core_params),
  1162. ICPU(0x47, core_params),
  1163. ICPU(0x4c, airmont_params),
  1164. ICPU(0x4e, core_params),
  1165. ICPU(0x4f, core_params),
  1166. ICPU(0x5e, core_params),
  1167. ICPU(0x56, core_params),
  1168. ICPU(0x57, knl_params),
  1169. {}
  1170. };
  1171. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  1172. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
  1173. ICPU(0x56, core_params),
  1174. {}
  1175. };
  1176. static int intel_pstate_init_cpu(unsigned int cpunum)
  1177. {
  1178. struct cpudata *cpu;
  1179. if (!all_cpu_data[cpunum])
  1180. all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
  1181. GFP_KERNEL);
  1182. if (!all_cpu_data[cpunum])
  1183. return -ENOMEM;
  1184. cpu = all_cpu_data[cpunum];
  1185. cpu->cpu = cpunum;
  1186. if (hwp_active) {
  1187. intel_pstate_hwp_enable(cpu);
  1188. pid_params.sample_rate_ms = 50;
  1189. pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
  1190. }
  1191. intel_pstate_get_cpu_pstates(cpu);
  1192. intel_pstate_busy_pid_reset(cpu);
  1193. pr_debug("controlling: cpu %d\n", cpunum);
  1194. return 0;
  1195. }
  1196. static unsigned int intel_pstate_get(unsigned int cpu_num)
  1197. {
  1198. struct cpudata *cpu = all_cpu_data[cpu_num];
  1199. return cpu ? get_avg_frequency(cpu) : 0;
  1200. }
  1201. static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
  1202. {
  1203. struct cpudata *cpu = all_cpu_data[cpu_num];
  1204. /* Prevent intel_pstate_update_util() from using stale data. */
  1205. cpu->sample.time = 0;
  1206. cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
  1207. intel_pstate_update_util);
  1208. cpu->update_util_set = true;
  1209. }
  1210. static void intel_pstate_clear_update_util_hook(unsigned int cpu)
  1211. {
  1212. struct cpudata *cpu_data = all_cpu_data[cpu];
  1213. if (!cpu_data->update_util_set)
  1214. return;
  1215. cpufreq_remove_update_util_hook(cpu);
  1216. cpu_data->update_util_set = false;
  1217. synchronize_sched();
  1218. }
  1219. static void intel_pstate_set_performance_limits(struct perf_limits *limits)
  1220. {
  1221. limits->no_turbo = 0;
  1222. limits->turbo_disabled = 0;
  1223. limits->max_perf_pct = 100;
  1224. limits->max_perf = int_tofp(1);
  1225. limits->min_perf_pct = 100;
  1226. limits->min_perf = int_tofp(1);
  1227. limits->max_policy_pct = 100;
  1228. limits->max_sysfs_pct = 100;
  1229. limits->min_policy_pct = 0;
  1230. limits->min_sysfs_pct = 0;
  1231. }
  1232. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  1233. {
  1234. struct cpudata *cpu;
  1235. if (!policy->cpuinfo.max_freq)
  1236. return -ENODEV;
  1237. intel_pstate_clear_update_util_hook(policy->cpu);
  1238. cpu = all_cpu_data[0];
  1239. if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
  1240. policy->max < policy->cpuinfo.max_freq &&
  1241. policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
  1242. pr_debug("policy->max > max non turbo frequency\n");
  1243. policy->max = policy->cpuinfo.max_freq;
  1244. }
  1245. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  1246. limits = &performance_limits;
  1247. if (policy->max >= policy->cpuinfo.max_freq) {
  1248. pr_debug("set performance\n");
  1249. intel_pstate_set_performance_limits(limits);
  1250. goto out;
  1251. }
  1252. } else {
  1253. pr_debug("set powersave\n");
  1254. limits = &powersave_limits;
  1255. }
  1256. limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
  1257. limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
  1258. limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
  1259. policy->cpuinfo.max_freq);
  1260. limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
  1261. /* Normalize user input to [min_policy_pct, max_policy_pct] */
  1262. limits->min_perf_pct = max(limits->min_policy_pct,
  1263. limits->min_sysfs_pct);
  1264. limits->min_perf_pct = min(limits->max_policy_pct,
  1265. limits->min_perf_pct);
  1266. limits->max_perf_pct = min(limits->max_policy_pct,
  1267. limits->max_sysfs_pct);
  1268. limits->max_perf_pct = max(limits->min_policy_pct,
  1269. limits->max_perf_pct);
  1270. limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
  1271. /* Make sure min_perf_pct <= max_perf_pct */
  1272. limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
  1273. limits->min_perf = div_fp(limits->min_perf_pct, 100);
  1274. limits->max_perf = div_fp(limits->max_perf_pct, 100);
  1275. out:
  1276. intel_pstate_set_update_util_hook(policy->cpu);
  1277. intel_pstate_hwp_set_policy(policy);
  1278. return 0;
  1279. }
  1280. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  1281. {
  1282. cpufreq_verify_within_cpu_limits(policy);
  1283. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  1284. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  1285. return -EINVAL;
  1286. return 0;
  1287. }
  1288. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  1289. {
  1290. int cpu_num = policy->cpu;
  1291. struct cpudata *cpu = all_cpu_data[cpu_num];
  1292. pr_debug("CPU %d exiting\n", cpu_num);
  1293. intel_pstate_clear_update_util_hook(cpu_num);
  1294. if (hwp_active)
  1295. return;
  1296. intel_pstate_set_min_pstate(cpu);
  1297. }
  1298. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1299. {
  1300. struct cpudata *cpu;
  1301. int rc;
  1302. rc = intel_pstate_init_cpu(policy->cpu);
  1303. if (rc)
  1304. return rc;
  1305. cpu = all_cpu_data[policy->cpu];
  1306. if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
  1307. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1308. else
  1309. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1310. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1311. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1312. /* cpuinfo and default policy values */
  1313. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1314. policy->cpuinfo.max_freq =
  1315. cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1316. intel_pstate_init_acpi_perf_limits(policy);
  1317. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  1318. cpumask_set_cpu(policy->cpu, policy->cpus);
  1319. return 0;
  1320. }
  1321. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  1322. {
  1323. intel_pstate_exit_perf_limits(policy);
  1324. return 0;
  1325. }
  1326. static struct cpufreq_driver intel_pstate_driver = {
  1327. .flags = CPUFREQ_CONST_LOOPS,
  1328. .verify = intel_pstate_verify_policy,
  1329. .setpolicy = intel_pstate_set_policy,
  1330. .resume = intel_pstate_hwp_set_policy,
  1331. .get = intel_pstate_get,
  1332. .init = intel_pstate_cpu_init,
  1333. .exit = intel_pstate_cpu_exit,
  1334. .stop_cpu = intel_pstate_stop_cpu,
  1335. .name = "intel_pstate",
  1336. };
  1337. static int __initdata no_load;
  1338. static int __initdata no_hwp;
  1339. static int __initdata hwp_only;
  1340. static unsigned int force_load;
  1341. static int intel_pstate_msrs_not_valid(void)
  1342. {
  1343. if (!pstate_funcs.get_max() ||
  1344. !pstate_funcs.get_min() ||
  1345. !pstate_funcs.get_turbo())
  1346. return -ENODEV;
  1347. return 0;
  1348. }
  1349. static void copy_pid_params(struct pstate_adjust_policy *policy)
  1350. {
  1351. pid_params.sample_rate_ms = policy->sample_rate_ms;
  1352. pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
  1353. pid_params.p_gain_pct = policy->p_gain_pct;
  1354. pid_params.i_gain_pct = policy->i_gain_pct;
  1355. pid_params.d_gain_pct = policy->d_gain_pct;
  1356. pid_params.deadband = policy->deadband;
  1357. pid_params.setpoint = policy->setpoint;
  1358. }
  1359. static void copy_cpu_funcs(struct pstate_funcs *funcs)
  1360. {
  1361. pstate_funcs.get_max = funcs->get_max;
  1362. pstate_funcs.get_max_physical = funcs->get_max_physical;
  1363. pstate_funcs.get_min = funcs->get_min;
  1364. pstate_funcs.get_turbo = funcs->get_turbo;
  1365. pstate_funcs.get_scaling = funcs->get_scaling;
  1366. pstate_funcs.get_val = funcs->get_val;
  1367. pstate_funcs.get_vid = funcs->get_vid;
  1368. pstate_funcs.get_target_pstate = funcs->get_target_pstate;
  1369. }
  1370. #ifdef CONFIG_ACPI
  1371. static bool intel_pstate_no_acpi_pss(void)
  1372. {
  1373. int i;
  1374. for_each_possible_cpu(i) {
  1375. acpi_status status;
  1376. union acpi_object *pss;
  1377. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1378. struct acpi_processor *pr = per_cpu(processors, i);
  1379. if (!pr)
  1380. continue;
  1381. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  1382. if (ACPI_FAILURE(status))
  1383. continue;
  1384. pss = buffer.pointer;
  1385. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  1386. kfree(pss);
  1387. return false;
  1388. }
  1389. kfree(pss);
  1390. }
  1391. return true;
  1392. }
  1393. static bool intel_pstate_has_acpi_ppc(void)
  1394. {
  1395. int i;
  1396. for_each_possible_cpu(i) {
  1397. struct acpi_processor *pr = per_cpu(processors, i);
  1398. if (!pr)
  1399. continue;
  1400. if (acpi_has_method(pr->handle, "_PPC"))
  1401. return true;
  1402. }
  1403. return false;
  1404. }
  1405. enum {
  1406. PSS,
  1407. PPC,
  1408. };
  1409. struct hw_vendor_info {
  1410. u16 valid;
  1411. char oem_id[ACPI_OEM_ID_SIZE];
  1412. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  1413. int oem_pwr_table;
  1414. };
  1415. /* Hardware vendor-specific info that has its own power management modes */
  1416. static struct hw_vendor_info vendor_info[] = {
  1417. {1, "HP ", "ProLiant", PSS},
  1418. {1, "ORACLE", "X4-2 ", PPC},
  1419. {1, "ORACLE", "X4-2L ", PPC},
  1420. {1, "ORACLE", "X4-2B ", PPC},
  1421. {1, "ORACLE", "X3-2 ", PPC},
  1422. {1, "ORACLE", "X3-2L ", PPC},
  1423. {1, "ORACLE", "X3-2B ", PPC},
  1424. {1, "ORACLE", "X4470M2 ", PPC},
  1425. {1, "ORACLE", "X4270M3 ", PPC},
  1426. {1, "ORACLE", "X4270M2 ", PPC},
  1427. {1, "ORACLE", "X4170M2 ", PPC},
  1428. {1, "ORACLE", "X4170 M3", PPC},
  1429. {1, "ORACLE", "X4275 M3", PPC},
  1430. {1, "ORACLE", "X6-2 ", PPC},
  1431. {1, "ORACLE", "Sudbury ", PPC},
  1432. {0, "", ""},
  1433. };
  1434. static bool intel_pstate_platform_pwr_mgmt_exists(void)
  1435. {
  1436. struct acpi_table_header hdr;
  1437. struct hw_vendor_info *v_info;
  1438. const struct x86_cpu_id *id;
  1439. u64 misc_pwr;
  1440. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  1441. if (id) {
  1442. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  1443. if ( misc_pwr & (1 << 8))
  1444. return true;
  1445. }
  1446. if (acpi_disabled ||
  1447. ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
  1448. return false;
  1449. for (v_info = vendor_info; v_info->valid; v_info++) {
  1450. if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
  1451. !strncmp(hdr.oem_table_id, v_info->oem_table_id,
  1452. ACPI_OEM_TABLE_ID_SIZE))
  1453. switch (v_info->oem_pwr_table) {
  1454. case PSS:
  1455. return intel_pstate_no_acpi_pss();
  1456. case PPC:
  1457. return intel_pstate_has_acpi_ppc() &&
  1458. (!force_load);
  1459. }
  1460. }
  1461. return false;
  1462. }
  1463. #else /* CONFIG_ACPI not enabled */
  1464. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  1465. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  1466. #endif /* CONFIG_ACPI */
  1467. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  1468. { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
  1469. {}
  1470. };
  1471. static int __init intel_pstate_init(void)
  1472. {
  1473. int cpu, rc = 0;
  1474. const struct x86_cpu_id *id;
  1475. struct cpu_defaults *cpu_def;
  1476. if (no_load)
  1477. return -ENODEV;
  1478. if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
  1479. copy_cpu_funcs(&core_params.funcs);
  1480. hwp_active++;
  1481. goto hwp_cpu_matched;
  1482. }
  1483. id = x86_match_cpu(intel_pstate_cpu_ids);
  1484. if (!id)
  1485. return -ENODEV;
  1486. cpu_def = (struct cpu_defaults *)id->driver_data;
  1487. copy_pid_params(&cpu_def->pid_policy);
  1488. copy_cpu_funcs(&cpu_def->funcs);
  1489. if (intel_pstate_msrs_not_valid())
  1490. return -ENODEV;
  1491. hwp_cpu_matched:
  1492. /*
  1493. * The Intel pstate driver will be ignored if the platform
  1494. * firmware has its own power management modes.
  1495. */
  1496. if (intel_pstate_platform_pwr_mgmt_exists())
  1497. return -ENODEV;
  1498. pr_info("Intel P-state driver initializing\n");
  1499. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  1500. if (!all_cpu_data)
  1501. return -ENOMEM;
  1502. if (!hwp_active && hwp_only)
  1503. goto out;
  1504. rc = cpufreq_register_driver(&intel_pstate_driver);
  1505. if (rc)
  1506. goto out;
  1507. intel_pstate_debug_expose_params();
  1508. intel_pstate_sysfs_expose_params();
  1509. if (hwp_active)
  1510. pr_info("HWP enabled\n");
  1511. return rc;
  1512. out:
  1513. get_online_cpus();
  1514. for_each_online_cpu(cpu) {
  1515. if (all_cpu_data[cpu]) {
  1516. intel_pstate_clear_update_util_hook(cpu);
  1517. kfree(all_cpu_data[cpu]);
  1518. }
  1519. }
  1520. put_online_cpus();
  1521. vfree(all_cpu_data);
  1522. return -ENODEV;
  1523. }
  1524. device_initcall(intel_pstate_init);
  1525. static int __init intel_pstate_setup(char *str)
  1526. {
  1527. if (!str)
  1528. return -EINVAL;
  1529. if (!strcmp(str, "disable"))
  1530. no_load = 1;
  1531. if (!strcmp(str, "no_hwp")) {
  1532. pr_info("HWP disabled\n");
  1533. no_hwp = 1;
  1534. }
  1535. if (!strcmp(str, "force"))
  1536. force_load = 1;
  1537. if (!strcmp(str, "hwp_only"))
  1538. hwp_only = 1;
  1539. #ifdef CONFIG_ACPI
  1540. if (!strcmp(str, "support_acpi_ppc"))
  1541. acpi_ppc = true;
  1542. #endif
  1543. return 0;
  1544. }
  1545. early_param("intel_pstate", intel_pstate_setup);
  1546. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  1547. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  1548. MODULE_LICENSE("GPL");