intel_pstate.c 49 KB

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