intel_pstate.c 33 KB

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