intel_pstate.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 <trace/events/power.h>
  28. #include <asm/div64.h>
  29. #include <asm/msr.h>
  30. #include <asm/cpu_device_id.h>
  31. #define SAMPLE_COUNT 3
  32. #define FRAC_BITS 8
  33. #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  34. #define fp_toint(X) ((X) >> FRAC_BITS)
  35. static inline int32_t mul_fp(int32_t x, int32_t y)
  36. {
  37. return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  38. }
  39. static inline int32_t div_fp(int32_t x, int32_t y)
  40. {
  41. return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
  42. }
  43. struct sample {
  44. int core_pct_busy;
  45. u64 aperf;
  46. u64 mperf;
  47. int freq;
  48. };
  49. struct pstate_data {
  50. int current_pstate;
  51. int min_pstate;
  52. int max_pstate;
  53. int turbo_pstate;
  54. };
  55. struct _pid {
  56. int setpoint;
  57. int32_t integral;
  58. int32_t p_gain;
  59. int32_t i_gain;
  60. int32_t d_gain;
  61. int deadband;
  62. int last_err;
  63. };
  64. struct cpudata {
  65. int cpu;
  66. char name[64];
  67. struct timer_list timer;
  68. struct pstate_data pstate;
  69. struct _pid pid;
  70. int min_pstate_count;
  71. u64 prev_aperf;
  72. u64 prev_mperf;
  73. int sample_ptr;
  74. struct sample samples[SAMPLE_COUNT];
  75. };
  76. static struct cpudata **all_cpu_data;
  77. struct pstate_adjust_policy {
  78. int sample_rate_ms;
  79. int deadband;
  80. int setpoint;
  81. int p_gain_pct;
  82. int d_gain_pct;
  83. int i_gain_pct;
  84. };
  85. struct pstate_funcs {
  86. int (*get_max)(void);
  87. int (*get_min)(void);
  88. int (*get_turbo)(void);
  89. void (*set)(int pstate);
  90. };
  91. struct cpu_defaults {
  92. struct pstate_adjust_policy pid_policy;
  93. struct pstate_funcs funcs;
  94. };
  95. static struct pstate_adjust_policy pid_params;
  96. static struct pstate_funcs pstate_funcs;
  97. struct perf_limits {
  98. int no_turbo;
  99. int max_perf_pct;
  100. int min_perf_pct;
  101. int32_t max_perf;
  102. int32_t min_perf;
  103. int max_policy_pct;
  104. int max_sysfs_pct;
  105. };
  106. static struct perf_limits limits = {
  107. .no_turbo = 0,
  108. .max_perf_pct = 100,
  109. .max_perf = int_tofp(1),
  110. .min_perf_pct = 0,
  111. .min_perf = 0,
  112. .max_policy_pct = 100,
  113. .max_sysfs_pct = 100,
  114. };
  115. static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
  116. int deadband, int integral) {
  117. pid->setpoint = setpoint;
  118. pid->deadband = deadband;
  119. pid->integral = int_tofp(integral);
  120. pid->last_err = setpoint - busy;
  121. }
  122. static inline void pid_p_gain_set(struct _pid *pid, int percent)
  123. {
  124. pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
  125. }
  126. static inline void pid_i_gain_set(struct _pid *pid, int percent)
  127. {
  128. pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
  129. }
  130. static inline void pid_d_gain_set(struct _pid *pid, int percent)
  131. {
  132. pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
  133. }
  134. static signed int pid_calc(struct _pid *pid, int busy)
  135. {
  136. signed int err, result;
  137. int32_t pterm, dterm, fp_error;
  138. int32_t integral_limit;
  139. err = pid->setpoint - busy;
  140. fp_error = int_tofp(err);
  141. if (abs(err) <= pid->deadband)
  142. return 0;
  143. pterm = mul_fp(pid->p_gain, fp_error);
  144. pid->integral += fp_error;
  145. /* limit the integral term */
  146. integral_limit = int_tofp(30);
  147. if (pid->integral > integral_limit)
  148. pid->integral = integral_limit;
  149. if (pid->integral < -integral_limit)
  150. pid->integral = -integral_limit;
  151. dterm = mul_fp(pid->d_gain, (err - pid->last_err));
  152. pid->last_err = err;
  153. result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
  154. return (signed int)fp_toint(result);
  155. }
  156. static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
  157. {
  158. pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
  159. pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
  160. pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
  161. pid_reset(&cpu->pid,
  162. pid_params.setpoint,
  163. 100,
  164. pid_params.deadband,
  165. 0);
  166. }
  167. static inline void intel_pstate_reset_all_pid(void)
  168. {
  169. unsigned int cpu;
  170. for_each_online_cpu(cpu) {
  171. if (all_cpu_data[cpu])
  172. intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
  173. }
  174. }
  175. /************************** debugfs begin ************************/
  176. static int pid_param_set(void *data, u64 val)
  177. {
  178. *(u32 *)data = val;
  179. intel_pstate_reset_all_pid();
  180. return 0;
  181. }
  182. static int pid_param_get(void *data, u64 *val)
  183. {
  184. *val = *(u32 *)data;
  185. return 0;
  186. }
  187. DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
  188. pid_param_set, "%llu\n");
  189. struct pid_param {
  190. char *name;
  191. void *value;
  192. };
  193. static struct pid_param pid_files[] = {
  194. {"sample_rate_ms", &pid_params.sample_rate_ms},
  195. {"d_gain_pct", &pid_params.d_gain_pct},
  196. {"i_gain_pct", &pid_params.i_gain_pct},
  197. {"deadband", &pid_params.deadband},
  198. {"setpoint", &pid_params.setpoint},
  199. {"p_gain_pct", &pid_params.p_gain_pct},
  200. {NULL, NULL}
  201. };
  202. static struct dentry *debugfs_parent;
  203. static void intel_pstate_debug_expose_params(void)
  204. {
  205. int i = 0;
  206. debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
  207. if (IS_ERR_OR_NULL(debugfs_parent))
  208. return;
  209. while (pid_files[i].name) {
  210. debugfs_create_file(pid_files[i].name, 0660,
  211. debugfs_parent, pid_files[i].value,
  212. &fops_pid_param);
  213. i++;
  214. }
  215. }
  216. /************************** debugfs end ************************/
  217. /************************** sysfs begin ************************/
  218. #define show_one(file_name, object) \
  219. static ssize_t show_##file_name \
  220. (struct kobject *kobj, struct attribute *attr, char *buf) \
  221. { \
  222. return sprintf(buf, "%u\n", limits.object); \
  223. }
  224. static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
  225. const char *buf, size_t count)
  226. {
  227. unsigned int input;
  228. int ret;
  229. ret = sscanf(buf, "%u", &input);
  230. if (ret != 1)
  231. return -EINVAL;
  232. limits.no_turbo = clamp_t(int, input, 0 , 1);
  233. return count;
  234. }
  235. static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
  236. const char *buf, size_t count)
  237. {
  238. unsigned int input;
  239. int ret;
  240. ret = sscanf(buf, "%u", &input);
  241. if (ret != 1)
  242. return -EINVAL;
  243. limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
  244. limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
  245. limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
  246. return count;
  247. }
  248. static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
  249. const char *buf, size_t count)
  250. {
  251. unsigned int input;
  252. int ret;
  253. ret = sscanf(buf, "%u", &input);
  254. if (ret != 1)
  255. return -EINVAL;
  256. limits.min_perf_pct = clamp_t(int, input, 0 , 100);
  257. limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
  258. return count;
  259. }
  260. show_one(no_turbo, no_turbo);
  261. show_one(max_perf_pct, max_perf_pct);
  262. show_one(min_perf_pct, min_perf_pct);
  263. define_one_global_rw(no_turbo);
  264. define_one_global_rw(max_perf_pct);
  265. define_one_global_rw(min_perf_pct);
  266. static struct attribute *intel_pstate_attributes[] = {
  267. &no_turbo.attr,
  268. &max_perf_pct.attr,
  269. &min_perf_pct.attr,
  270. NULL
  271. };
  272. static struct attribute_group intel_pstate_attr_group = {
  273. .attrs = intel_pstate_attributes,
  274. };
  275. static struct kobject *intel_pstate_kobject;
  276. static void intel_pstate_sysfs_expose_params(void)
  277. {
  278. int rc;
  279. intel_pstate_kobject = kobject_create_and_add("intel_pstate",
  280. &cpu_subsys.dev_root->kobj);
  281. BUG_ON(!intel_pstate_kobject);
  282. rc = sysfs_create_group(intel_pstate_kobject,
  283. &intel_pstate_attr_group);
  284. BUG_ON(rc);
  285. }
  286. /************************** sysfs end ************************/
  287. static int core_get_min_pstate(void)
  288. {
  289. u64 value;
  290. rdmsrl(MSR_PLATFORM_INFO, value);
  291. return (value >> 40) & 0xFF;
  292. }
  293. static int core_get_max_pstate(void)
  294. {
  295. u64 value;
  296. rdmsrl(MSR_PLATFORM_INFO, value);
  297. return (value >> 8) & 0xFF;
  298. }
  299. static int core_get_turbo_pstate(void)
  300. {
  301. u64 value;
  302. int nont, ret;
  303. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  304. nont = core_get_max_pstate();
  305. ret = ((value) & 255);
  306. if (ret <= nont)
  307. ret = nont;
  308. return ret;
  309. }
  310. static void core_set_pstate(int pstate)
  311. {
  312. u64 val;
  313. val = pstate << 8;
  314. if (limits.no_turbo)
  315. val |= (u64)1 << 32;
  316. wrmsrl(MSR_IA32_PERF_CTL, val);
  317. }
  318. static struct cpu_defaults core_params = {
  319. .pid_policy = {
  320. .sample_rate_ms = 10,
  321. .deadband = 0,
  322. .setpoint = 97,
  323. .p_gain_pct = 20,
  324. .d_gain_pct = 0,
  325. .i_gain_pct = 0,
  326. },
  327. .funcs = {
  328. .get_max = core_get_max_pstate,
  329. .get_min = core_get_min_pstate,
  330. .get_turbo = core_get_turbo_pstate,
  331. .set = core_set_pstate,
  332. },
  333. };
  334. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  335. {
  336. int max_perf = cpu->pstate.turbo_pstate;
  337. int min_perf;
  338. if (limits.no_turbo)
  339. max_perf = cpu->pstate.max_pstate;
  340. max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
  341. *max = clamp_t(int, max_perf,
  342. cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
  343. min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
  344. *min = clamp_t(int, min_perf,
  345. cpu->pstate.min_pstate, max_perf);
  346. }
  347. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
  348. {
  349. int max_perf, min_perf;
  350. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  351. pstate = clamp_t(int, pstate, min_perf, max_perf);
  352. if (pstate == cpu->pstate.current_pstate)
  353. return;
  354. trace_cpu_frequency(pstate * 100000, cpu->cpu);
  355. cpu->pstate.current_pstate = pstate;
  356. pstate_funcs.set(pstate);
  357. }
  358. static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
  359. {
  360. int target;
  361. target = cpu->pstate.current_pstate + steps;
  362. intel_pstate_set_pstate(cpu, target);
  363. }
  364. static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
  365. {
  366. int target;
  367. target = cpu->pstate.current_pstate - steps;
  368. intel_pstate_set_pstate(cpu, target);
  369. }
  370. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  371. {
  372. sprintf(cpu->name, "Intel 2nd generation core");
  373. cpu->pstate.min_pstate = pstate_funcs.get_min();
  374. cpu->pstate.max_pstate = pstate_funcs.get_max();
  375. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  376. /*
  377. * goto max pstate so we don't slow up boot if we are built-in if we are
  378. * a module we will take care of it during normal operation
  379. */
  380. intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
  381. }
  382. static inline void intel_pstate_calc_busy(struct cpudata *cpu,
  383. struct sample *sample)
  384. {
  385. u64 core_pct;
  386. core_pct = div64_u64(sample->aperf * 100, sample->mperf);
  387. sample->freq = cpu->pstate.max_pstate * core_pct * 1000;
  388. sample->core_pct_busy = core_pct;
  389. }
  390. static inline void intel_pstate_sample(struct cpudata *cpu)
  391. {
  392. u64 aperf, mperf;
  393. rdmsrl(MSR_IA32_APERF, aperf);
  394. rdmsrl(MSR_IA32_MPERF, mperf);
  395. cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
  396. cpu->samples[cpu->sample_ptr].aperf = aperf;
  397. cpu->samples[cpu->sample_ptr].mperf = mperf;
  398. cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
  399. cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
  400. intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
  401. cpu->prev_aperf = aperf;
  402. cpu->prev_mperf = mperf;
  403. }
  404. static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
  405. {
  406. int sample_time, delay;
  407. sample_time = pid_params.sample_rate_ms;
  408. delay = msecs_to_jiffies(sample_time);
  409. mod_timer_pinned(&cpu->timer, jiffies + delay);
  410. }
  411. static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu)
  412. {
  413. int32_t busy_scaled;
  414. int32_t core_busy, max_pstate, current_pstate;
  415. core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy);
  416. max_pstate = int_tofp(cpu->pstate.max_pstate);
  417. current_pstate = int_tofp(cpu->pstate.current_pstate);
  418. busy_scaled = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
  419. return fp_toint(busy_scaled);
  420. }
  421. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  422. {
  423. int busy_scaled;
  424. struct _pid *pid;
  425. signed int ctl = 0;
  426. int steps;
  427. pid = &cpu->pid;
  428. busy_scaled = intel_pstate_get_scaled_busy(cpu);
  429. ctl = pid_calc(pid, busy_scaled);
  430. steps = abs(ctl);
  431. if (ctl < 0)
  432. intel_pstate_pstate_increase(cpu, steps);
  433. else
  434. intel_pstate_pstate_decrease(cpu, steps);
  435. }
  436. static void intel_pstate_timer_func(unsigned long __data)
  437. {
  438. struct cpudata *cpu = (struct cpudata *) __data;
  439. intel_pstate_sample(cpu);
  440. intel_pstate_adjust_busy_pstate(cpu);
  441. if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
  442. cpu->min_pstate_count++;
  443. if (!(cpu->min_pstate_count % 5)) {
  444. intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
  445. }
  446. } else
  447. cpu->min_pstate_count = 0;
  448. intel_pstate_set_sample_time(cpu);
  449. }
  450. #define ICPU(model, policy) \
  451. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy }
  452. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  453. ICPU(0x2a, core_params),
  454. ICPU(0x2d, core_params),
  455. ICPU(0x3a, core_params),
  456. ICPU(0x3c, core_params),
  457. ICPU(0x3e, core_params),
  458. ICPU(0x3f, core_params),
  459. ICPU(0x45, core_params),
  460. ICPU(0x46, core_params),
  461. {}
  462. };
  463. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  464. static int intel_pstate_init_cpu(unsigned int cpunum)
  465. {
  466. const struct x86_cpu_id *id;
  467. struct cpudata *cpu;
  468. id = x86_match_cpu(intel_pstate_cpu_ids);
  469. if (!id)
  470. return -ENODEV;
  471. all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
  472. if (!all_cpu_data[cpunum])
  473. return -ENOMEM;
  474. cpu = all_cpu_data[cpunum];
  475. intel_pstate_get_cpu_pstates(cpu);
  476. cpu->cpu = cpunum;
  477. init_timer_deferrable(&cpu->timer);
  478. cpu->timer.function = intel_pstate_timer_func;
  479. cpu->timer.data =
  480. (unsigned long)cpu;
  481. cpu->timer.expires = jiffies + HZ/100;
  482. intel_pstate_busy_pid_reset(cpu);
  483. intel_pstate_sample(cpu);
  484. intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
  485. add_timer_on(&cpu->timer, cpunum);
  486. pr_info("Intel pstate controlling: cpu %d\n", cpunum);
  487. return 0;
  488. }
  489. static unsigned int intel_pstate_get(unsigned int cpu_num)
  490. {
  491. struct sample *sample;
  492. struct cpudata *cpu;
  493. cpu = all_cpu_data[cpu_num];
  494. if (!cpu)
  495. return 0;
  496. sample = &cpu->samples[cpu->sample_ptr];
  497. return sample->freq;
  498. }
  499. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  500. {
  501. struct cpudata *cpu;
  502. cpu = all_cpu_data[policy->cpu];
  503. if (!policy->cpuinfo.max_freq)
  504. return -ENODEV;
  505. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  506. limits.min_perf_pct = 100;
  507. limits.min_perf = int_tofp(1);
  508. limits.max_perf_pct = 100;
  509. limits.max_perf = int_tofp(1);
  510. limits.no_turbo = 0;
  511. return 0;
  512. }
  513. limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
  514. limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
  515. limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
  516. limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
  517. limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
  518. limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
  519. limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
  520. return 0;
  521. }
  522. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  523. {
  524. cpufreq_verify_within_cpu_limits(policy);
  525. if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
  526. (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
  527. return -EINVAL;
  528. return 0;
  529. }
  530. static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
  531. {
  532. int cpu = policy->cpu;
  533. del_timer(&all_cpu_data[cpu]->timer);
  534. kfree(all_cpu_data[cpu]);
  535. all_cpu_data[cpu] = NULL;
  536. return 0;
  537. }
  538. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  539. {
  540. struct cpudata *cpu;
  541. int rc;
  542. rc = intel_pstate_init_cpu(policy->cpu);
  543. if (rc)
  544. return rc;
  545. cpu = all_cpu_data[policy->cpu];
  546. if (!limits.no_turbo &&
  547. limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
  548. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  549. else
  550. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  551. policy->min = cpu->pstate.min_pstate * 100000;
  552. policy->max = cpu->pstate.turbo_pstate * 100000;
  553. /* cpuinfo and default policy values */
  554. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
  555. policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
  556. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  557. cpumask_set_cpu(policy->cpu, policy->cpus);
  558. return 0;
  559. }
  560. static struct cpufreq_driver intel_pstate_driver = {
  561. .flags = CPUFREQ_CONST_LOOPS,
  562. .verify = intel_pstate_verify_policy,
  563. .setpolicy = intel_pstate_set_policy,
  564. .get = intel_pstate_get,
  565. .init = intel_pstate_cpu_init,
  566. .exit = intel_pstate_cpu_exit,
  567. .name = "intel_pstate",
  568. };
  569. static int __initdata no_load;
  570. static int intel_pstate_msrs_not_valid(void)
  571. {
  572. /* Check that all the msr's we are using are valid. */
  573. u64 aperf, mperf, tmp;
  574. rdmsrl(MSR_IA32_APERF, aperf);
  575. rdmsrl(MSR_IA32_MPERF, mperf);
  576. if (!pstate_funcs.get_max() ||
  577. !pstate_funcs.get_min() ||
  578. !pstate_funcs.get_turbo())
  579. return -ENODEV;
  580. rdmsrl(MSR_IA32_APERF, tmp);
  581. if (!(tmp - aperf))
  582. return -ENODEV;
  583. rdmsrl(MSR_IA32_MPERF, tmp);
  584. if (!(tmp - mperf))
  585. return -ENODEV;
  586. return 0;
  587. }
  588. void copy_pid_params(struct pstate_adjust_policy *policy)
  589. {
  590. pid_params.sample_rate_ms = policy->sample_rate_ms;
  591. pid_params.p_gain_pct = policy->p_gain_pct;
  592. pid_params.i_gain_pct = policy->i_gain_pct;
  593. pid_params.d_gain_pct = policy->d_gain_pct;
  594. pid_params.deadband = policy->deadband;
  595. pid_params.setpoint = policy->setpoint;
  596. }
  597. void copy_cpu_funcs(struct pstate_funcs *funcs)
  598. {
  599. pstate_funcs.get_max = funcs->get_max;
  600. pstate_funcs.get_min = funcs->get_min;
  601. pstate_funcs.get_turbo = funcs->get_turbo;
  602. pstate_funcs.set = funcs->set;
  603. }
  604. static int __init intel_pstate_init(void)
  605. {
  606. int cpu, rc = 0;
  607. const struct x86_cpu_id *id;
  608. struct cpu_defaults *cpu_info;
  609. if (no_load)
  610. return -ENODEV;
  611. id = x86_match_cpu(intel_pstate_cpu_ids);
  612. if (!id)
  613. return -ENODEV;
  614. cpu_info = (struct cpu_defaults *)id->driver_data;
  615. copy_pid_params(&cpu_info->pid_policy);
  616. copy_cpu_funcs(&cpu_info->funcs);
  617. if (intel_pstate_msrs_not_valid())
  618. return -ENODEV;
  619. pr_info("Intel P-state driver initializing.\n");
  620. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  621. if (!all_cpu_data)
  622. return -ENOMEM;
  623. rc = cpufreq_register_driver(&intel_pstate_driver);
  624. if (rc)
  625. goto out;
  626. intel_pstate_debug_expose_params();
  627. intel_pstate_sysfs_expose_params();
  628. return rc;
  629. out:
  630. get_online_cpus();
  631. for_each_online_cpu(cpu) {
  632. if (all_cpu_data[cpu]) {
  633. del_timer_sync(&all_cpu_data[cpu]->timer);
  634. kfree(all_cpu_data[cpu]);
  635. }
  636. }
  637. put_online_cpus();
  638. vfree(all_cpu_data);
  639. return -ENODEV;
  640. }
  641. device_initcall(intel_pstate_init);
  642. static int __init intel_pstate_setup(char *str)
  643. {
  644. if (!str)
  645. return -EINVAL;
  646. if (!strcmp(str, "disable"))
  647. no_load = 1;
  648. return 0;
  649. }
  650. early_param("intel_pstate", intel_pstate_setup);
  651. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  652. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  653. MODULE_LICENSE("GPL");