intel_pstate.c 22 KB

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