intel_pstate.c 19 KB

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