intel_pstate.c 35 KB

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