intel_pstate.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. #include <linux/kernel.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/module.h>
  15. #include <linux/ktime.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/tick.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/list.h>
  21. #include <linux/cpu.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/types.h>
  25. #include <linux/fs.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/acpi.h>
  28. #include <linux/vmalloc.h>
  29. #include <trace/events/power.h>
  30. #include <asm/div64.h>
  31. #include <asm/msr.h>
  32. #include <asm/cpu_device_id.h>
  33. #include <asm/cpufeature.h>
  34. #if IS_ENABLED(CONFIG_ACPI)
  35. #include <acpi/processor.h>
  36. #endif
  37. #define BYT_RATIOS 0x66a
  38. #define BYT_VIDS 0x66b
  39. #define BYT_TURBO_RATIOS 0x66c
  40. #define BYT_TURBO_VIDS 0x66d
  41. #define FRAC_BITS 8
  42. #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  43. #define fp_toint(X) ((X) >> FRAC_BITS)
  44. static inline int32_t mul_fp(int32_t x, int32_t y)
  45. {
  46. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  47. }
  48. static inline int32_t div_fp(s64 x, s64 y)
  49. {
  50. return div64_s64((int64_t)x << FRAC_BITS, y);
  51. }
  52. static inline int ceiling_fp(int32_t x)
  53. {
  54. int mask, ret;
  55. ret = fp_toint(x);
  56. mask = (1 << FRAC_BITS) - 1;
  57. if (x & mask)
  58. ret += 1;
  59. return ret;
  60. }
  61. struct sample {
  62. int32_t core_pct_busy;
  63. u64 aperf;
  64. u64 mperf;
  65. u64 tsc;
  66. int freq;
  67. ktime_t time;
  68. };
  69. struct pstate_data {
  70. int current_pstate;
  71. int min_pstate;
  72. int max_pstate;
  73. int max_pstate_physical;
  74. int scaling;
  75. int turbo_pstate;
  76. };
  77. struct vid_data {
  78. int min;
  79. int max;
  80. int turbo;
  81. int32_t ratio;
  82. };
  83. struct _pid {
  84. int setpoint;
  85. int32_t integral;
  86. int32_t p_gain;
  87. int32_t i_gain;
  88. int32_t d_gain;
  89. int deadband;
  90. int32_t last_err;
  91. };
  92. struct cpudata {
  93. int cpu;
  94. struct timer_list timer;
  95. struct pstate_data pstate;
  96. struct vid_data vid;
  97. struct _pid pid;
  98. ktime_t last_sample_time;
  99. u64 prev_aperf;
  100. u64 prev_mperf;
  101. u64 prev_tsc;
  102. struct sample sample;
  103. #if IS_ENABLED(CONFIG_ACPI)
  104. struct acpi_processor_performance acpi_perf_data;
  105. #endif
  106. };
  107. static struct cpudata **all_cpu_data;
  108. struct pstate_adjust_policy {
  109. int sample_rate_ms;
  110. int deadband;
  111. int setpoint;
  112. int p_gain_pct;
  113. int d_gain_pct;
  114. int i_gain_pct;
  115. };
  116. struct pstate_funcs {
  117. int (*get_max)(void);
  118. int (*get_max_physical)(void);
  119. int (*get_min)(void);
  120. int (*get_turbo)(void);
  121. int (*get_scaling)(void);
  122. void (*set)(struct cpudata*, int pstate);
  123. void (*get_vid)(struct cpudata *);
  124. };
  125. struct cpu_defaults {
  126. struct pstate_adjust_policy pid_policy;
  127. struct pstate_funcs funcs;
  128. };
  129. static struct pstate_adjust_policy pid_params;
  130. static struct pstate_funcs pstate_funcs;
  131. static int hwp_active;
  132. static int no_acpi_perf;
  133. struct perf_limits {
  134. int no_turbo;
  135. int turbo_disabled;
  136. int max_perf_pct;
  137. int min_perf_pct;
  138. int32_t max_perf;
  139. int32_t min_perf;
  140. int max_policy_pct;
  141. int max_sysfs_pct;
  142. int min_policy_pct;
  143. int min_sysfs_pct;
  144. int max_perf_ctl;
  145. int min_perf_ctl;
  146. };
  147. static struct perf_limits performance_limits = {
  148. .no_turbo = 0,
  149. .turbo_disabled = 0,
  150. .max_perf_pct = 100,
  151. .max_perf = int_tofp(1),
  152. .min_perf_pct = 100,
  153. .min_perf = int_tofp(1),
  154. .max_policy_pct = 100,
  155. .max_sysfs_pct = 100,
  156. .min_policy_pct = 0,
  157. .min_sysfs_pct = 0,
  158. };
  159. static struct perf_limits powersave_limits = {
  160. .no_turbo = 0,
  161. .turbo_disabled = 0,
  162. .max_perf_pct = 100,
  163. .max_perf = int_tofp(1),
  164. .min_perf_pct = 0,
  165. .min_perf = 0,
  166. .max_policy_pct = 100,
  167. .max_sysfs_pct = 100,
  168. .min_policy_pct = 0,
  169. .min_sysfs_pct = 0,
  170. .max_perf_ctl = 0,
  171. .min_perf_ctl = 0,
  172. };
  173. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  174. static struct perf_limits *limits = &performance_limits;
  175. #else
  176. static struct perf_limits *limits = &powersave_limits;
  177. #endif
  178. #if IS_ENABLED(CONFIG_ACPI)
  179. /*
  180. * The max target pstate ratio is a 8 bit value in both PLATFORM_INFO MSR and
  181. * in TURBO_RATIO_LIMIT MSR, which pstate driver stores in max_pstate and
  182. * max_turbo_pstate fields. The PERF_CTL MSR contains 16 bit value for P state
  183. * ratio, out of it only high 8 bits are used. For example 0x1700 is setting
  184. * target ratio 0x17. The _PSS control value stores in a format which can be
  185. * directly written to PERF_CTL MSR. But in intel_pstate driver this shift
  186. * occurs during write to PERF_CTL (E.g. for cores core_set_pstate()).
  187. * This function converts the _PSS control value to intel pstate driver format
  188. * for comparison and assignment.
  189. */
  190. static int convert_to_native_pstate_format(struct cpudata *cpu, int index)
  191. {
  192. return cpu->acpi_perf_data.states[index].control >> 8;
  193. }
  194. static int intel_pstate_init_perf_limits(struct cpufreq_policy *policy)
  195. {
  196. struct cpudata *cpu;
  197. int ret;
  198. bool turbo_absent = false;
  199. int max_pstate_index;
  200. int min_pss_ctl, max_pss_ctl, turbo_pss_ctl;
  201. int i;
  202. cpu = all_cpu_data[policy->cpu];
  203. pr_debug("intel_pstate: default limits 0x%x 0x%x 0x%x\n",
  204. cpu->pstate.min_pstate, cpu->pstate.max_pstate,
  205. cpu->pstate.turbo_pstate);
  206. if (!cpu->acpi_perf_data.shared_cpu_map &&
  207. zalloc_cpumask_var_node(&cpu->acpi_perf_data.shared_cpu_map,
  208. GFP_KERNEL, cpu_to_node(policy->cpu))) {
  209. return -ENOMEM;
  210. }
  211. ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
  212. policy->cpu);
  213. if (ret)
  214. return ret;
  215. /*
  216. * Check if the control value in _PSS is for PERF_CTL MSR, which should
  217. * guarantee that the states returned by it map to the states in our
  218. * list directly.
  219. */
  220. if (cpu->acpi_perf_data.control_register.space_id !=
  221. ACPI_ADR_SPACE_FIXED_HARDWARE)
  222. return -EIO;
  223. pr_debug("intel_pstate: CPU%u - ACPI _PSS perf data\n", policy->cpu);
  224. for (i = 0; i < cpu->acpi_perf_data.state_count; i++)
  225. pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
  226. (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
  227. (u32) cpu->acpi_perf_data.states[i].core_frequency,
  228. (u32) cpu->acpi_perf_data.states[i].power,
  229. (u32) cpu->acpi_perf_data.states[i].control);
  230. /*
  231. * If there is only one entry _PSS, simply ignore _PSS and continue as
  232. * usual without taking _PSS into account
  233. */
  234. if (cpu->acpi_perf_data.state_count < 2)
  235. return 0;
  236. turbo_pss_ctl = convert_to_native_pstate_format(cpu, 0);
  237. min_pss_ctl = convert_to_native_pstate_format(cpu,
  238. cpu->acpi_perf_data.state_count - 1);
  239. /* Check if there is a turbo freq in _PSS */
  240. if (turbo_pss_ctl <= cpu->pstate.max_pstate &&
  241. turbo_pss_ctl > cpu->pstate.min_pstate) {
  242. pr_debug("intel_pstate: no turbo range exists in _PSS\n");
  243. limits->no_turbo = limits->turbo_disabled = 1;
  244. cpu->pstate.turbo_pstate = cpu->pstate.max_pstate;
  245. turbo_absent = true;
  246. }
  247. /* Check if the max non turbo p state < Intel P state max */
  248. max_pstate_index = turbo_absent ? 0 : 1;
  249. max_pss_ctl = convert_to_native_pstate_format(cpu, max_pstate_index);
  250. if (max_pss_ctl < cpu->pstate.max_pstate &&
  251. max_pss_ctl > cpu->pstate.min_pstate)
  252. cpu->pstate.max_pstate = max_pss_ctl;
  253. /* check If min perf > Intel P State min */
  254. if (min_pss_ctl > cpu->pstate.min_pstate &&
  255. min_pss_ctl < cpu->pstate.max_pstate) {
  256. cpu->pstate.min_pstate = min_pss_ctl;
  257. policy->cpuinfo.min_freq = min_pss_ctl * cpu->pstate.scaling;
  258. }
  259. if (turbo_absent)
  260. policy->cpuinfo.max_freq = cpu->pstate.max_pstate *
  261. cpu->pstate.scaling;
  262. else {
  263. policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate *
  264. cpu->pstate.scaling;
  265. /*
  266. * The _PSS table doesn't contain whole turbo frequency range.
  267. * This just contains +1 MHZ above the max non turbo frequency,
  268. * with control value corresponding to max turbo ratio. But
  269. * when cpufreq set policy is called, it will call with this
  270. * max frequency, which will cause a reduced performance as
  271. * this driver uses real max turbo frequency as the max
  272. * frequeny. So correct this frequency in _PSS table to
  273. * correct max turbo frequency based on the turbo ratio.
  274. * Also need to convert to MHz as _PSS freq is in MHz.
  275. */
  276. cpu->acpi_perf_data.states[0].core_frequency =
  277. turbo_pss_ctl * 100;
  278. }
  279. pr_debug("intel_pstate: Updated limits using _PSS 0x%x 0x%x 0x%x\n",
  280. cpu->pstate.min_pstate, cpu->pstate.max_pstate,
  281. cpu->pstate.turbo_pstate);
  282. pr_debug("intel_pstate: policy max_freq=%d Khz min_freq = %d KHz\n",
  283. policy->cpuinfo.max_freq, policy->cpuinfo.min_freq);
  284. return 0;
  285. }
  286. static int intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  287. {
  288. struct cpudata *cpu;
  289. if (!no_acpi_perf)
  290. return 0;
  291. cpu = all_cpu_data[policy->cpu];
  292. acpi_processor_unregister_performance(policy->cpu);
  293. return 0;
  294. }
  295. #else
  296. static int intel_pstate_init_perf_limits(struct cpufreq_policy *policy)
  297. {
  298. return 0;
  299. }
  300. static int intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
  301. {
  302. return 0;
  303. }
  304. #endif
  305. static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
  306. int deadband, int integral) {
  307. pid->setpoint = setpoint;
  308. pid->deadband = deadband;
  309. pid->integral = int_tofp(integral);
  310. pid->last_err = int_tofp(setpoint) - int_tofp(busy);
  311. }
  312. static inline void pid_p_gain_set(struct _pid *pid, int percent)
  313. {
  314. pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
  315. }
  316. static inline void pid_i_gain_set(struct _pid *pid, int percent)
  317. {
  318. pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
  319. }
  320. static inline void pid_d_gain_set(struct _pid *pid, int percent)
  321. {
  322. pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
  323. }
  324. static signed int pid_calc(struct _pid *pid, int32_t busy)
  325. {
  326. signed int result;
  327. int32_t pterm, dterm, fp_error;
  328. int32_t integral_limit;
  329. fp_error = int_tofp(pid->setpoint) - busy;
  330. if (abs(fp_error) <= int_tofp(pid->deadband))
  331. return 0;
  332. pterm = mul_fp(pid->p_gain, fp_error);
  333. pid->integral += fp_error;
  334. /*
  335. * We limit the integral here so that it will never
  336. * get higher than 30. This prevents it from becoming
  337. * too large an input over long periods of time and allows
  338. * it to get factored out sooner.
  339. *
  340. * The value of 30 was chosen through experimentation.
  341. */
  342. integral_limit = int_tofp(30);
  343. if (pid->integral > integral_limit)
  344. pid->integral = integral_limit;
  345. if (pid->integral < -integral_limit)
  346. pid->integral = -integral_limit;
  347. dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
  348. pid->last_err = fp_error;
  349. result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
  350. result = result + (1 << (FRAC_BITS-1));
  351. return (signed int)fp_toint(result);
  352. }
  353. static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
  354. {
  355. pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
  356. pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
  357. pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
  358. pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
  359. }
  360. static inline void intel_pstate_reset_all_pid(void)
  361. {
  362. unsigned int cpu;
  363. for_each_online_cpu(cpu) {
  364. if (all_cpu_data[cpu])
  365. intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
  366. }
  367. }
  368. static inline void update_turbo_state(void)
  369. {
  370. u64 misc_en;
  371. struct cpudata *cpu;
  372. cpu = all_cpu_data[0];
  373. rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
  374. limits->turbo_disabled =
  375. (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
  376. cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
  377. }
  378. static void intel_pstate_hwp_set(void)
  379. {
  380. int min, hw_min, max, hw_max, cpu, range, adj_range;
  381. u64 value, cap;
  382. rdmsrl(MSR_HWP_CAPABILITIES, cap);
  383. hw_min = HWP_LOWEST_PERF(cap);
  384. hw_max = HWP_HIGHEST_PERF(cap);
  385. range = hw_max - hw_min;
  386. get_online_cpus();
  387. for_each_online_cpu(cpu) {
  388. rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
  389. adj_range = limits->min_perf_pct * range / 100;
  390. min = hw_min + adj_range;
  391. value &= ~HWP_MIN_PERF(~0L);
  392. value |= HWP_MIN_PERF(min);
  393. adj_range = limits->max_perf_pct * range / 100;
  394. max = hw_min + adj_range;
  395. if (limits->no_turbo) {
  396. hw_max = HWP_GUARANTEED_PERF(cap);
  397. if (hw_max < max)
  398. max = hw_max;
  399. }
  400. value &= ~HWP_MAX_PERF(~0L);
  401. value |= HWP_MAX_PERF(max);
  402. wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
  403. }
  404. put_online_cpus();
  405. }
  406. /************************** debugfs begin ************************/
  407. static int pid_param_set(void *data, u64 val)
  408. {
  409. *(u32 *)data = val;
  410. intel_pstate_reset_all_pid();
  411. return 0;
  412. }
  413. static int pid_param_get(void *data, u64 *val)
  414. {
  415. *val = *(u32 *)data;
  416. return 0;
  417. }
  418. DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
  419. struct pid_param {
  420. char *name;
  421. void *value;
  422. };
  423. static struct pid_param pid_files[] = {
  424. {"sample_rate_ms", &pid_params.sample_rate_ms},
  425. {"d_gain_pct", &pid_params.d_gain_pct},
  426. {"i_gain_pct", &pid_params.i_gain_pct},
  427. {"deadband", &pid_params.deadband},
  428. {"setpoint", &pid_params.setpoint},
  429. {"p_gain_pct", &pid_params.p_gain_pct},
  430. {NULL, NULL}
  431. };
  432. static void __init intel_pstate_debug_expose_params(void)
  433. {
  434. struct dentry *debugfs_parent;
  435. int i = 0;
  436. if (hwp_active)
  437. return;
  438. debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
  439. if (IS_ERR_OR_NULL(debugfs_parent))
  440. return;
  441. while (pid_files[i].name) {
  442. debugfs_create_file(pid_files[i].name, 0660,
  443. debugfs_parent, pid_files[i].value,
  444. &fops_pid_param);
  445. i++;
  446. }
  447. }
  448. /************************** debugfs end ************************/
  449. /************************** sysfs begin ************************/
  450. #define show_one(file_name, object) \
  451. static ssize_t show_##file_name \
  452. (struct kobject *kobj, struct attribute *attr, char *buf) \
  453. { \
  454. return sprintf(buf, "%u\n", limits->object); \
  455. }
  456. static ssize_t show_turbo_pct(struct kobject *kobj,
  457. struct attribute *attr, char *buf)
  458. {
  459. struct cpudata *cpu;
  460. int total, no_turbo, turbo_pct;
  461. uint32_t turbo_fp;
  462. cpu = all_cpu_data[0];
  463. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  464. no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
  465. turbo_fp = div_fp(int_tofp(no_turbo), int_tofp(total));
  466. turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
  467. return sprintf(buf, "%u\n", turbo_pct);
  468. }
  469. static ssize_t show_num_pstates(struct kobject *kobj,
  470. struct attribute *attr, char *buf)
  471. {
  472. struct cpudata *cpu;
  473. int total;
  474. cpu = all_cpu_data[0];
  475. total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
  476. return sprintf(buf, "%u\n", total);
  477. }
  478. static ssize_t show_no_turbo(struct kobject *kobj,
  479. struct attribute *attr, char *buf)
  480. {
  481. ssize_t ret;
  482. update_turbo_state();
  483. if (limits->turbo_disabled)
  484. ret = sprintf(buf, "%u\n", limits->turbo_disabled);
  485. else
  486. ret = sprintf(buf, "%u\n", limits->no_turbo);
  487. return ret;
  488. }
  489. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  490. const char *buf, size_t count)
  491. {
  492. unsigned int input;
  493. int ret;
  494. ret = sscanf(buf, "%u", &input);
  495. if (ret != 1)
  496. return -EINVAL;
  497. update_turbo_state();
  498. if (limits->turbo_disabled) {
  499. pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n");
  500. return -EPERM;
  501. }
  502. limits->no_turbo = clamp_t(int, input, 0, 1);
  503. if (hwp_active)
  504. intel_pstate_hwp_set();
  505. return count;
  506. }
  507. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  508. const char *buf, size_t count)
  509. {
  510. unsigned int input;
  511. int ret;
  512. ret = sscanf(buf, "%u", &input);
  513. if (ret != 1)
  514. return -EINVAL;
  515. limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
  516. limits->max_perf_pct = min(limits->max_policy_pct,
  517. limits->max_sysfs_pct);
  518. limits->max_perf_pct = max(limits->min_policy_pct,
  519. limits->max_perf_pct);
  520. limits->max_perf_pct = max(limits->min_perf_pct,
  521. limits->max_perf_pct);
  522. limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
  523. int_tofp(100));
  524. if (hwp_active)
  525. intel_pstate_hwp_set();
  526. return count;
  527. }
  528. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  529. const char *buf, size_t count)
  530. {
  531. unsigned int input;
  532. int ret;
  533. ret = sscanf(buf, "%u", &input);
  534. if (ret != 1)
  535. return -EINVAL;
  536. limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
  537. limits->min_perf_pct = max(limits->min_policy_pct,
  538. limits->min_sysfs_pct);
  539. limits->min_perf_pct = min(limits->max_policy_pct,
  540. limits->min_perf_pct);
  541. limits->min_perf_pct = min(limits->max_perf_pct,
  542. limits->min_perf_pct);
  543. limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
  544. int_tofp(100));
  545. if (hwp_active)
  546. intel_pstate_hwp_set();
  547. return count;
  548. }
  549. show_one(max_perf_pct, max_perf_pct);
  550. show_one(min_perf_pct, min_perf_pct);
  551. define_one_global_rw(no_turbo);
  552. define_one_global_rw(max_perf_pct);
  553. define_one_global_rw(min_perf_pct);
  554. define_one_global_ro(turbo_pct);
  555. define_one_global_ro(num_pstates);
  556. static struct attribute *intel_pstate_attributes[] = {
  557. &no_turbo.attr,
  558. &max_perf_pct.attr,
  559. &min_perf_pct.attr,
  560. &turbo_pct.attr,
  561. &num_pstates.attr,
  562. NULL
  563. };
  564. static struct attribute_group intel_pstate_attr_group = {
  565. .attrs = intel_pstate_attributes,
  566. };
  567. static void __init intel_pstate_sysfs_expose_params(void)
  568. {
  569. struct kobject *intel_pstate_kobject;
  570. int rc;
  571. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  572. &cpu_subsys.dev_root->kobj);
  573. BUG_ON(!intel_pstate_kobject);
  574. rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
  575. BUG_ON(rc);
  576. }
  577. /************************** sysfs end ************************/
  578. static void intel_pstate_hwp_enable(struct cpudata *cpudata)
  579. {
  580. pr_info("intel_pstate: HWP enabled\n");
  581. wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
  582. }
  583. static int byt_get_min_pstate(void)
  584. {
  585. u64 value;
  586. rdmsrl(BYT_RATIOS, value);
  587. return (value >> 8) & 0x7F;
  588. }
  589. static int byt_get_max_pstate(void)
  590. {
  591. u64 value;
  592. rdmsrl(BYT_RATIOS, value);
  593. return (value >> 16) & 0x7F;
  594. }
  595. static int byt_get_turbo_pstate(void)
  596. {
  597. u64 value;
  598. rdmsrl(BYT_TURBO_RATIOS, value);
  599. return value & 0x7F;
  600. }
  601. static void byt_set_pstate(struct cpudata *cpudata, int pstate)
  602. {
  603. u64 val;
  604. int32_t vid_fp;
  605. u32 vid;
  606. val = (u64)pstate << 8;
  607. if (limits->no_turbo && !limits->turbo_disabled)
  608. val |= (u64)1 << 32;
  609. vid_fp = cpudata->vid.min + mul_fp(
  610. int_tofp(pstate - cpudata->pstate.min_pstate),
  611. cpudata->vid.ratio);
  612. vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
  613. vid = ceiling_fp(vid_fp);
  614. if (pstate > cpudata->pstate.max_pstate)
  615. vid = cpudata->vid.turbo;
  616. val |= vid;
  617. wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
  618. }
  619. #define BYT_BCLK_FREQS 5
  620. static int byt_freq_table[BYT_BCLK_FREQS] = { 833, 1000, 1333, 1167, 800};
  621. static int byt_get_scaling(void)
  622. {
  623. u64 value;
  624. int i;
  625. rdmsrl(MSR_FSB_FREQ, value);
  626. i = value & 0x3;
  627. BUG_ON(i > BYT_BCLK_FREQS);
  628. return byt_freq_table[i] * 100;
  629. }
  630. static void byt_get_vid(struct cpudata *cpudata)
  631. {
  632. u64 value;
  633. rdmsrl(BYT_VIDS, value);
  634. cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
  635. cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
  636. cpudata->vid.ratio = div_fp(
  637. cpudata->vid.max - cpudata->vid.min,
  638. int_tofp(cpudata->pstate.max_pstate -
  639. cpudata->pstate.min_pstate));
  640. rdmsrl(BYT_TURBO_VIDS, value);
  641. cpudata->vid.turbo = value & 0x7f;
  642. }
  643. static int core_get_min_pstate(void)
  644. {
  645. u64 value;
  646. rdmsrl(MSR_PLATFORM_INFO, value);
  647. return (value >> 40) & 0xFF;
  648. }
  649. static int core_get_max_pstate_physical(void)
  650. {
  651. u64 value;
  652. rdmsrl(MSR_PLATFORM_INFO, value);
  653. return (value >> 8) & 0xFF;
  654. }
  655. static int core_get_max_pstate(void)
  656. {
  657. u64 tar;
  658. u64 plat_info;
  659. int max_pstate;
  660. int err;
  661. rdmsrl(MSR_PLATFORM_INFO, plat_info);
  662. max_pstate = (plat_info >> 8) & 0xFF;
  663. err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
  664. if (!err) {
  665. /* Do some sanity checking for safety */
  666. if (plat_info & 0x600000000) {
  667. u64 tdp_ctrl;
  668. u64 tdp_ratio;
  669. int tdp_msr;
  670. err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
  671. if (err)
  672. goto skip_tar;
  673. tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
  674. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  675. if (err)
  676. goto skip_tar;
  677. if (tdp_ratio - 1 == tar) {
  678. max_pstate = tar;
  679. pr_debug("max_pstate=TAC %x\n", max_pstate);
  680. } else {
  681. goto skip_tar;
  682. }
  683. }
  684. }
  685. skip_tar:
  686. return max_pstate;
  687. }
  688. static int core_get_turbo_pstate(void)
  689. {
  690. u64 value;
  691. int nont, ret;
  692. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  693. nont = core_get_max_pstate();
  694. ret = (value) & 255;
  695. if (ret <= nont)
  696. ret = nont;
  697. return ret;
  698. }
  699. static inline int core_get_scaling(void)
  700. {
  701. return 100000;
  702. }
  703. static void core_set_pstate(struct cpudata *cpudata, int pstate)
  704. {
  705. u64 val;
  706. val = (u64)pstate << 8;
  707. if (limits->no_turbo && !limits->turbo_disabled)
  708. val |= (u64)1 << 32;
  709. wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
  710. }
  711. static int knl_get_turbo_pstate(void)
  712. {
  713. u64 value;
  714. int nont, ret;
  715. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  716. nont = core_get_max_pstate();
  717. ret = (((value) >> 8) & 0xFF);
  718. if (ret <= nont)
  719. ret = nont;
  720. return ret;
  721. }
  722. static struct cpu_defaults core_params = {
  723. .pid_policy = {
  724. .sample_rate_ms = 10,
  725. .deadband = 0,
  726. .setpoint = 97,
  727. .p_gain_pct = 20,
  728. .d_gain_pct = 0,
  729. .i_gain_pct = 0,
  730. },
  731. .funcs = {
  732. .get_max = core_get_max_pstate,
  733. .get_max_physical = core_get_max_pstate_physical,
  734. .get_min = core_get_min_pstate,
  735. .get_turbo = core_get_turbo_pstate,
  736. .get_scaling = core_get_scaling,
  737. .set = core_set_pstate,
  738. },
  739. };
  740. static struct cpu_defaults byt_params = {
  741. .pid_policy = {
  742. .sample_rate_ms = 10,
  743. .deadband = 0,
  744. .setpoint = 60,
  745. .p_gain_pct = 14,
  746. .d_gain_pct = 0,
  747. .i_gain_pct = 4,
  748. },
  749. .funcs = {
  750. .get_max = byt_get_max_pstate,
  751. .get_max_physical = byt_get_max_pstate,
  752. .get_min = byt_get_min_pstate,
  753. .get_turbo = byt_get_turbo_pstate,
  754. .set = byt_set_pstate,
  755. .get_scaling = byt_get_scaling,
  756. .get_vid = byt_get_vid,
  757. },
  758. };
  759. static struct cpu_defaults knl_params = {
  760. .pid_policy = {
  761. .sample_rate_ms = 10,
  762. .deadband = 0,
  763. .setpoint = 97,
  764. .p_gain_pct = 20,
  765. .d_gain_pct = 0,
  766. .i_gain_pct = 0,
  767. },
  768. .funcs = {
  769. .get_max = core_get_max_pstate,
  770. .get_max_physical = core_get_max_pstate_physical,
  771. .get_min = core_get_min_pstate,
  772. .get_turbo = knl_get_turbo_pstate,
  773. .get_scaling = core_get_scaling,
  774. .set = core_set_pstate,
  775. },
  776. };
  777. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  778. {
  779. int max_perf = cpu->pstate.turbo_pstate;
  780. int max_perf_adj;
  781. int min_perf;
  782. if (limits->no_turbo || limits->turbo_disabled)
  783. max_perf = cpu->pstate.max_pstate;
  784. /*
  785. * performance can be limited by user through sysfs, by cpufreq
  786. * policy, or by cpu specific default values determined through
  787. * experimentation.
  788. */
  789. if (limits->max_perf_ctl && limits->max_sysfs_pct >=
  790. limits->max_policy_pct) {
  791. *max = limits->max_perf_ctl;
  792. } else {
  793. max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf),
  794. limits->max_perf));
  795. *max = clamp_t(int, max_perf_adj, cpu->pstate.min_pstate,
  796. cpu->pstate.turbo_pstate);
  797. }
  798. if (limits->min_perf_ctl) {
  799. *min = limits->min_perf_ctl;
  800. } else {
  801. min_perf = fp_toint(mul_fp(int_tofp(max_perf),
  802. limits->min_perf));
  803. *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
  804. }
  805. }
  806. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force)
  807. {
  808. int max_perf, min_perf;
  809. if (force) {
  810. update_turbo_state();
  811. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  812. pstate = clamp_t(int, pstate, min_perf, max_perf);
  813. if (pstate == cpu->pstate.current_pstate)
  814. return;
  815. }
  816. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  817. cpu->pstate.current_pstate = pstate;
  818. pstate_funcs.set(cpu, pstate);
  819. }
  820. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  821. {
  822. cpu->pstate.min_pstate = pstate_funcs.get_min();
  823. cpu->pstate.max_pstate = pstate_funcs.get_max();
  824. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  825. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  826. cpu->pstate.scaling = pstate_funcs.get_scaling();
  827. if (pstate_funcs.get_vid)
  828. pstate_funcs.get_vid(cpu);
  829. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
  830. }
  831. static inline void intel_pstate_calc_busy(struct cpudata *cpu)
  832. {
  833. struct sample *sample = &cpu->sample;
  834. int64_t core_pct;
  835. core_pct = int_tofp(sample->aperf) * int_tofp(100);
  836. core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
  837. sample->freq = fp_toint(
  838. mul_fp(int_tofp(
  839. cpu->pstate.max_pstate_physical *
  840. cpu->pstate.scaling / 100),
  841. core_pct));
  842. sample->core_pct_busy = (int32_t)core_pct;
  843. }
  844. static inline void intel_pstate_sample(struct cpudata *cpu)
  845. {
  846. u64 aperf, mperf;
  847. unsigned long flags;
  848. u64 tsc;
  849. local_irq_save(flags);
  850. rdmsrl(MSR_IA32_APERF, aperf);
  851. rdmsrl(MSR_IA32_MPERF, mperf);
  852. if (cpu->prev_mperf == mperf) {
  853. local_irq_restore(flags);
  854. return;
  855. }
  856. tsc = rdtsc();
  857. local_irq_restore(flags);
  858. cpu->last_sample_time = cpu->sample.time;
  859. cpu->sample.time = ktime_get();
  860. cpu->sample.aperf = aperf;
  861. cpu->sample.mperf = mperf;
  862. cpu->sample.tsc = tsc;
  863. cpu->sample.aperf -= cpu->prev_aperf;
  864. cpu->sample.mperf -= cpu->prev_mperf;
  865. cpu->sample.tsc -= cpu->prev_tsc;
  866. intel_pstate_calc_busy(cpu);
  867. cpu->prev_aperf = aperf;
  868. cpu->prev_mperf = mperf;
  869. cpu->prev_tsc = tsc;
  870. }
  871. static inline void intel_hwp_set_sample_time(struct cpudata *cpu)
  872. {
  873. int delay;
  874. delay = msecs_to_jiffies(50);
  875. mod_timer_pinned(&cpu->timer, jiffies + delay);
  876. }
  877. static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
  878. {
  879. int delay;
  880. delay = msecs_to_jiffies(pid_params.sample_rate_ms);
  881. mod_timer_pinned(&cpu->timer, jiffies + delay);
  882. }
  883. static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
  884. {
  885. int32_t core_busy, max_pstate, current_pstate, sample_ratio;
  886. s64 duration_us;
  887. u32 sample_time;
  888. /*
  889. * core_busy is the ratio of actual performance to max
  890. * max_pstate is the max non turbo pstate available
  891. * current_pstate was the pstate that was requested during
  892. * the last sample period.
  893. *
  894. * We normalize core_busy, which was our actual percent
  895. * performance to what we requested during the last sample
  896. * period. The result will be a percentage of busy at a
  897. * specified pstate.
  898. */
  899. core_busy = cpu->sample.core_pct_busy;
  900. max_pstate = int_tofp(cpu->pstate.max_pstate_physical);
  901. current_pstate = int_tofp(cpu->pstate.current_pstate);
  902. core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
  903. /*
  904. * Since we have a deferred timer, it will not fire unless
  905. * we are in C0. So, determine if the actual elapsed time
  906. * is significantly greater (3x) than our sample interval. If it
  907. * is, then we were idle for a long enough period of time
  908. * to adjust our busyness.
  909. */
  910. sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC;
  911. duration_us = ktime_us_delta(cpu->sample.time,
  912. cpu->last_sample_time);
  913. if (duration_us > sample_time * 3) {
  914. sample_ratio = div_fp(int_tofp(sample_time),
  915. int_tofp(duration_us));
  916. core_busy = mul_fp(core_busy, sample_ratio);
  917. }
  918. return core_busy;
  919. }
  920. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  921. {
  922. int32_t busy_scaled;
  923. struct _pid *pid;
  924. signed int ctl;
  925. int from;
  926. struct sample *sample;
  927. from = cpu->pstate.current_pstate;
  928. pid = &cpu->pid;
  929. busy_scaled = intel_pstate_get_scaled_busy(cpu);
  930. ctl = pid_calc(pid, busy_scaled);
  931. /* Negative values of ctl increase the pstate and vice versa */
  932. intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl, true);
  933. sample = &cpu->sample;
  934. trace_pstate_sample(fp_toint(sample->core_pct_busy),
  935. fp_toint(busy_scaled),
  936. from,
  937. cpu->pstate.current_pstate,
  938. sample->mperf,
  939. sample->aperf,
  940. sample->tsc,
  941. sample->freq);
  942. }
  943. static void intel_hwp_timer_func(unsigned long __data)
  944. {
  945. struct cpudata *cpu = (struct cpudata *) __data;
  946. intel_pstate_sample(cpu);
  947. intel_hwp_set_sample_time(cpu);
  948. }
  949. static void intel_pstate_timer_func(unsigned long __data)
  950. {
  951. struct cpudata *cpu = (struct cpudata *) __data;
  952. intel_pstate_sample(cpu);
  953. intel_pstate_adjust_busy_pstate(cpu);
  954. intel_pstate_set_sample_time(cpu);
  955. }
  956. #define ICPU(model, policy) \
  957. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  958. (unsigned long)&policy }
  959. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  960. ICPU(0x2a, core_params),
  961. ICPU(0x2d, core_params),
  962. ICPU(0x37, byt_params),
  963. ICPU(0x3a, core_params),
  964. ICPU(0x3c, core_params),
  965. ICPU(0x3d, core_params),
  966. ICPU(0x3e, core_params),
  967. ICPU(0x3f, core_params),
  968. ICPU(0x45, core_params),
  969. ICPU(0x46, core_params),
  970. ICPU(0x47, core_params),
  971. ICPU(0x4c, byt_params),
  972. ICPU(0x4e, core_params),
  973. ICPU(0x4f, core_params),
  974. ICPU(0x5e, core_params),
  975. ICPU(0x56, core_params),
  976. ICPU(0x57, knl_params),
  977. {}
  978. };
  979. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  980. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
  981. ICPU(0x56, core_params),
  982. {}
  983. };
  984. static int intel_pstate_init_cpu(unsigned int cpunum)
  985. {
  986. struct cpudata *cpu;
  987. if (!all_cpu_data[cpunum])
  988. all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
  989. GFP_KERNEL);
  990. if (!all_cpu_data[cpunum])
  991. return -ENOMEM;
  992. cpu = all_cpu_data[cpunum];
  993. cpu->cpu = cpunum;
  994. if (hwp_active)
  995. intel_pstate_hwp_enable(cpu);
  996. intel_pstate_get_cpu_pstates(cpu);
  997. init_timer_deferrable(&cpu->timer);
  998. cpu->timer.data = (unsigned long)cpu;
  999. cpu->timer.expires = jiffies + HZ/100;
  1000. if (!hwp_active)
  1001. cpu->timer.function = intel_pstate_timer_func;
  1002. else
  1003. cpu->timer.function = intel_hwp_timer_func;
  1004. intel_pstate_busy_pid_reset(cpu);
  1005. intel_pstate_sample(cpu);
  1006. add_timer_on(&cpu->timer, cpunum);
  1007. pr_debug("intel_pstate: controlling: cpu %d\n", cpunum);
  1008. return 0;
  1009. }
  1010. static unsigned int intel_pstate_get(unsigned int cpu_num)
  1011. {
  1012. struct sample *sample;
  1013. struct cpudata *cpu;
  1014. cpu = all_cpu_data[cpu_num];
  1015. if (!cpu)
  1016. return 0;
  1017. sample = &cpu->sample;
  1018. return sample->freq;
  1019. }
  1020. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  1021. {
  1022. #if IS_ENABLED(CONFIG_ACPI)
  1023. struct cpudata *cpu;
  1024. int i;
  1025. #endif
  1026. pr_debug("intel_pstate: %s max %u policy->max %u\n", __func__,
  1027. policy->cpuinfo.max_freq, policy->max);
  1028. if (!policy->cpuinfo.max_freq)
  1029. return -ENODEV;
  1030. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE &&
  1031. policy->max >= policy->cpuinfo.max_freq) {
  1032. pr_debug("intel_pstate: set performance\n");
  1033. limits = &performance_limits;
  1034. return 0;
  1035. }
  1036. pr_debug("intel_pstate: set powersave\n");
  1037. limits = &powersave_limits;
  1038. limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
  1039. limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
  1040. limits->max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
  1041. limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
  1042. /* Normalize user input to [min_policy_pct, max_policy_pct] */
  1043. limits->min_perf_pct = max(limits->min_policy_pct,
  1044. limits->min_sysfs_pct);
  1045. limits->min_perf_pct = min(limits->max_policy_pct,
  1046. limits->min_perf_pct);
  1047. limits->max_perf_pct = min(limits->max_policy_pct,
  1048. limits->max_sysfs_pct);
  1049. limits->max_perf_pct = max(limits->min_policy_pct,
  1050. limits->max_perf_pct);
  1051. /* Make sure min_perf_pct <= max_perf_pct */
  1052. limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
  1053. limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
  1054. int_tofp(100));
  1055. limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
  1056. int_tofp(100));
  1057. #if IS_ENABLED(CONFIG_ACPI)
  1058. cpu = all_cpu_data[policy->cpu];
  1059. for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
  1060. int control;
  1061. control = convert_to_native_pstate_format(cpu, i);
  1062. if (control * cpu->pstate.scaling == policy->max)
  1063. limits->max_perf_ctl = control;
  1064. if (control * cpu->pstate.scaling == policy->min)
  1065. limits->min_perf_ctl = control;
  1066. }
  1067. pr_debug("intel_pstate: max %u policy_max %u perf_ctl [0x%x-0x%x]\n",
  1068. policy->cpuinfo.max_freq, policy->max, limits->min_perf_ctl,
  1069. limits->max_perf_ctl);
  1070. #endif
  1071. if (hwp_active)
  1072. intel_pstate_hwp_set();
  1073. return 0;
  1074. }
  1075. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  1076. {
  1077. cpufreq_verify_within_cpu_limits(policy);
  1078. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  1079. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  1080. return -EINVAL;
  1081. return 0;
  1082. }
  1083. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  1084. {
  1085. int cpu_num = policy->cpu;
  1086. struct cpudata *cpu = all_cpu_data[cpu_num];
  1087. pr_debug("intel_pstate: CPU %d exiting\n", cpu_num);
  1088. del_timer_sync(&all_cpu_data[cpu_num]->timer);
  1089. if (hwp_active)
  1090. return;
  1091. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
  1092. }
  1093. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  1094. {
  1095. struct cpudata *cpu;
  1096. int rc;
  1097. rc = intel_pstate_init_cpu(policy->cpu);
  1098. if (rc)
  1099. return rc;
  1100. cpu = all_cpu_data[policy->cpu];
  1101. if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
  1102. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1103. else
  1104. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1105. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1106. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1107. /* cpuinfo and default policy values */
  1108. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  1109. policy->cpuinfo.max_freq =
  1110. cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  1111. if (!no_acpi_perf)
  1112. intel_pstate_init_perf_limits(policy);
  1113. /*
  1114. * If there is no acpi perf data or error, we ignore and use Intel P
  1115. * state calculated limits, So this is not fatal error.
  1116. */
  1117. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  1118. cpumask_set_cpu(policy->cpu, policy->cpus);
  1119. return 0;
  1120. }
  1121. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  1122. {
  1123. return intel_pstate_exit_perf_limits(policy);
  1124. }
  1125. static struct cpufreq_driver intel_pstate_driver = {
  1126. .flags = CPUFREQ_CONST_LOOPS,
  1127. .verify = intel_pstate_verify_policy,
  1128. .setpolicy = intel_pstate_set_policy,
  1129. .get = intel_pstate_get,
  1130. .init = intel_pstate_cpu_init,
  1131. .exit = intel_pstate_cpu_exit,
  1132. .stop_cpu = intel_pstate_stop_cpu,
  1133. .name = "intel_pstate",
  1134. };
  1135. static int __initdata no_load;
  1136. static int __initdata no_hwp;
  1137. static int __initdata hwp_only;
  1138. static unsigned int force_load;
  1139. static int intel_pstate_msrs_not_valid(void)
  1140. {
  1141. if (!pstate_funcs.get_max() ||
  1142. !pstate_funcs.get_min() ||
  1143. !pstate_funcs.get_turbo())
  1144. return -ENODEV;
  1145. return 0;
  1146. }
  1147. static void copy_pid_params(struct pstate_adjust_policy *policy)
  1148. {
  1149. pid_params.sample_rate_ms = policy->sample_rate_ms;
  1150. pid_params.p_gain_pct = policy->p_gain_pct;
  1151. pid_params.i_gain_pct = policy->i_gain_pct;
  1152. pid_params.d_gain_pct = policy->d_gain_pct;
  1153. pid_params.deadband = policy->deadband;
  1154. pid_params.setpoint = policy->setpoint;
  1155. }
  1156. static void copy_cpu_funcs(struct pstate_funcs *funcs)
  1157. {
  1158. pstate_funcs.get_max = funcs->get_max;
  1159. pstate_funcs.get_max_physical = funcs->get_max_physical;
  1160. pstate_funcs.get_min = funcs->get_min;
  1161. pstate_funcs.get_turbo = funcs->get_turbo;
  1162. pstate_funcs.get_scaling = funcs->get_scaling;
  1163. pstate_funcs.set = funcs->set;
  1164. pstate_funcs.get_vid = funcs->get_vid;
  1165. }
  1166. #if IS_ENABLED(CONFIG_ACPI)
  1167. static bool intel_pstate_no_acpi_pss(void)
  1168. {
  1169. int i;
  1170. for_each_possible_cpu(i) {
  1171. acpi_status status;
  1172. union acpi_object *pss;
  1173. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1174. struct acpi_processor *pr = per_cpu(processors, i);
  1175. if (!pr)
  1176. continue;
  1177. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  1178. if (ACPI_FAILURE(status))
  1179. continue;
  1180. pss = buffer.pointer;
  1181. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  1182. kfree(pss);
  1183. return false;
  1184. }
  1185. kfree(pss);
  1186. }
  1187. return true;
  1188. }
  1189. static bool intel_pstate_has_acpi_ppc(void)
  1190. {
  1191. int i;
  1192. for_each_possible_cpu(i) {
  1193. struct acpi_processor *pr = per_cpu(processors, i);
  1194. if (!pr)
  1195. continue;
  1196. if (acpi_has_method(pr->handle, "_PPC"))
  1197. return true;
  1198. }
  1199. return false;
  1200. }
  1201. enum {
  1202. PSS,
  1203. PPC,
  1204. };
  1205. struct hw_vendor_info {
  1206. u16 valid;
  1207. char oem_id[ACPI_OEM_ID_SIZE];
  1208. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  1209. int oem_pwr_table;
  1210. };
  1211. /* Hardware vendor-specific info that has its own power management modes */
  1212. static struct hw_vendor_info vendor_info[] = {
  1213. {1, "HP ", "ProLiant", PSS},
  1214. {1, "ORACLE", "X4-2 ", PPC},
  1215. {1, "ORACLE", "X4-2L ", PPC},
  1216. {1, "ORACLE", "X4-2B ", PPC},
  1217. {1, "ORACLE", "X3-2 ", PPC},
  1218. {1, "ORACLE", "X3-2L ", PPC},
  1219. {1, "ORACLE", "X3-2B ", PPC},
  1220. {1, "ORACLE", "X4470M2 ", PPC},
  1221. {1, "ORACLE", "X4270M3 ", PPC},
  1222. {1, "ORACLE", "X4270M2 ", PPC},
  1223. {1, "ORACLE", "X4170M2 ", PPC},
  1224. {1, "ORACLE", "X4170 M3", PPC},
  1225. {1, "ORACLE", "X4275 M3", PPC},
  1226. {1, "ORACLE", "X6-2 ", PPC},
  1227. {1, "ORACLE", "Sudbury ", PPC},
  1228. {0, "", ""},
  1229. };
  1230. static bool intel_pstate_platform_pwr_mgmt_exists(void)
  1231. {
  1232. struct acpi_table_header hdr;
  1233. struct hw_vendor_info *v_info;
  1234. const struct x86_cpu_id *id;
  1235. u64 misc_pwr;
  1236. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  1237. if (id) {
  1238. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  1239. if ( misc_pwr & (1 << 8))
  1240. return true;
  1241. }
  1242. if (acpi_disabled ||
  1243. ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
  1244. return false;
  1245. for (v_info = vendor_info; v_info->valid; v_info++) {
  1246. if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
  1247. !strncmp(hdr.oem_table_id, v_info->oem_table_id,
  1248. ACPI_OEM_TABLE_ID_SIZE))
  1249. switch (v_info->oem_pwr_table) {
  1250. case PSS:
  1251. return intel_pstate_no_acpi_pss();
  1252. case PPC:
  1253. return intel_pstate_has_acpi_ppc() &&
  1254. (!force_load);
  1255. }
  1256. }
  1257. return false;
  1258. }
  1259. #else /* CONFIG_ACPI not enabled */
  1260. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  1261. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  1262. #endif /* CONFIG_ACPI */
  1263. static int __init intel_pstate_init(void)
  1264. {
  1265. int cpu, rc = 0;
  1266. const struct x86_cpu_id *id;
  1267. struct cpu_defaults *cpu_def;
  1268. if (no_load)
  1269. return -ENODEV;
  1270. id = x86_match_cpu(intel_pstate_cpu_ids);
  1271. if (!id)
  1272. return -ENODEV;
  1273. /*
  1274. * The Intel pstate driver will be ignored if the platform
  1275. * firmware has its own power management modes.
  1276. */
  1277. if (intel_pstate_platform_pwr_mgmt_exists())
  1278. return -ENODEV;
  1279. cpu_def = (struct cpu_defaults *)id->driver_data;
  1280. copy_pid_params(&cpu_def->pid_policy);
  1281. copy_cpu_funcs(&cpu_def->funcs);
  1282. if (intel_pstate_msrs_not_valid())
  1283. return -ENODEV;
  1284. pr_info("Intel P-state driver initializing.\n");
  1285. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  1286. if (!all_cpu_data)
  1287. return -ENOMEM;
  1288. if (static_cpu_has_safe(X86_FEATURE_HWP) && !no_hwp)
  1289. hwp_active++;
  1290. if (!hwp_active && hwp_only)
  1291. goto out;
  1292. rc = cpufreq_register_driver(&intel_pstate_driver);
  1293. if (rc)
  1294. goto out;
  1295. intel_pstate_debug_expose_params();
  1296. intel_pstate_sysfs_expose_params();
  1297. return rc;
  1298. out:
  1299. get_online_cpus();
  1300. for_each_online_cpu(cpu) {
  1301. if (all_cpu_data[cpu]) {
  1302. del_timer_sync(&all_cpu_data[cpu]->timer);
  1303. kfree(all_cpu_data[cpu]);
  1304. }
  1305. }
  1306. put_online_cpus();
  1307. vfree(all_cpu_data);
  1308. return -ENODEV;
  1309. }
  1310. device_initcall(intel_pstate_init);
  1311. static int __init intel_pstate_setup(char *str)
  1312. {
  1313. if (!str)
  1314. return -EINVAL;
  1315. if (!strcmp(str, "disable"))
  1316. no_load = 1;
  1317. if (!strcmp(str, "no_hwp"))
  1318. no_hwp = 1;
  1319. if (!strcmp(str, "force"))
  1320. force_load = 1;
  1321. if (!strcmp(str, "hwp_only"))
  1322. hwp_only = 1;
  1323. if (!strcmp(str, "no_acpi"))
  1324. no_acpi_perf = 1;
  1325. return 0;
  1326. }
  1327. early_param("intel_pstate", intel_pstate_setup);
  1328. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  1329. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  1330. MODULE_LICENSE("GPL");