intel_pstate.c 19 KB

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