intel_pstate.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. get_online_cpus();
  245. for_each_online_cpu(cpu) {
  246. rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
  247. hw_min = HWP_LOWEST_PERF(cap);
  248. hw_max = HWP_HIGHEST_PERF(cap);
  249. range = hw_max - hw_min;
  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 kobj_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 kobj_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 kobj_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 kobj_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 kobj_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 kobj_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 kobj_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 & 0x3);
  549. err = rdmsrl_safe(tdp_msr, &tdp_ratio);
  550. if (err)
  551. goto skip_tar;
  552. /* For level 1 and 2, bits[23:16] contain the ratio */
  553. if (tdp_ctrl)
  554. tdp_ratio >>= 16;
  555. tdp_ratio &= 0xff; /* ratios are only 8 bits long */
  556. if (tdp_ratio - 1 == tar) {
  557. max_pstate = tar;
  558. pr_debug("max_pstate=TAC %x\n", max_pstate);
  559. } else {
  560. goto skip_tar;
  561. }
  562. }
  563. }
  564. skip_tar:
  565. return max_pstate;
  566. }
  567. static int core_get_turbo_pstate(void)
  568. {
  569. u64 value;
  570. int nont, ret;
  571. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  572. nont = core_get_max_pstate();
  573. ret = (value) & 255;
  574. if (ret <= nont)
  575. ret = nont;
  576. return ret;
  577. }
  578. static inline int core_get_scaling(void)
  579. {
  580. return 100000;
  581. }
  582. static void core_set_pstate(struct cpudata *cpudata, int pstate)
  583. {
  584. u64 val;
  585. val = (u64)pstate << 8;
  586. if (limits->no_turbo && !limits->turbo_disabled)
  587. val |= (u64)1 << 32;
  588. wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
  589. }
  590. static int knl_get_turbo_pstate(void)
  591. {
  592. u64 value;
  593. int nont, ret;
  594. rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
  595. nont = core_get_max_pstate();
  596. ret = (((value) >> 8) & 0xFF);
  597. if (ret <= nont)
  598. ret = nont;
  599. return ret;
  600. }
  601. static struct cpu_defaults core_params = {
  602. .pid_policy = {
  603. .sample_rate_ms = 10,
  604. .deadband = 0,
  605. .setpoint = 97,
  606. .p_gain_pct = 20,
  607. .d_gain_pct = 0,
  608. .i_gain_pct = 0,
  609. },
  610. .funcs = {
  611. .get_max = core_get_max_pstate,
  612. .get_max_physical = core_get_max_pstate_physical,
  613. .get_min = core_get_min_pstate,
  614. .get_turbo = core_get_turbo_pstate,
  615. .get_scaling = core_get_scaling,
  616. .set = core_set_pstate,
  617. },
  618. };
  619. static struct cpu_defaults silvermont_params = {
  620. .pid_policy = {
  621. .sample_rate_ms = 10,
  622. .deadband = 0,
  623. .setpoint = 60,
  624. .p_gain_pct = 14,
  625. .d_gain_pct = 0,
  626. .i_gain_pct = 4,
  627. },
  628. .funcs = {
  629. .get_max = atom_get_max_pstate,
  630. .get_max_physical = atom_get_max_pstate,
  631. .get_min = atom_get_min_pstate,
  632. .get_turbo = atom_get_turbo_pstate,
  633. .set = atom_set_pstate,
  634. .get_scaling = silvermont_get_scaling,
  635. .get_vid = atom_get_vid,
  636. },
  637. };
  638. static struct cpu_defaults airmont_params = {
  639. .pid_policy = {
  640. .sample_rate_ms = 10,
  641. .deadband = 0,
  642. .setpoint = 60,
  643. .p_gain_pct = 14,
  644. .d_gain_pct = 0,
  645. .i_gain_pct = 4,
  646. },
  647. .funcs = {
  648. .get_max = atom_get_max_pstate,
  649. .get_max_physical = atom_get_max_pstate,
  650. .get_min = atom_get_min_pstate,
  651. .get_turbo = atom_get_turbo_pstate,
  652. .set = atom_set_pstate,
  653. .get_scaling = airmont_get_scaling,
  654. .get_vid = atom_get_vid,
  655. },
  656. };
  657. static struct cpu_defaults knl_params = {
  658. .pid_policy = {
  659. .sample_rate_ms = 10,
  660. .deadband = 0,
  661. .setpoint = 97,
  662. .p_gain_pct = 20,
  663. .d_gain_pct = 0,
  664. .i_gain_pct = 0,
  665. },
  666. .funcs = {
  667. .get_max = core_get_max_pstate,
  668. .get_max_physical = core_get_max_pstate_physical,
  669. .get_min = core_get_min_pstate,
  670. .get_turbo = knl_get_turbo_pstate,
  671. .get_scaling = core_get_scaling,
  672. .set = core_set_pstate,
  673. },
  674. };
  675. static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
  676. {
  677. int max_perf = cpu->pstate.turbo_pstate;
  678. int max_perf_adj;
  679. int min_perf;
  680. if (limits->no_turbo || limits->turbo_disabled)
  681. max_perf = cpu->pstate.max_pstate;
  682. /*
  683. * performance can be limited by user through sysfs, by cpufreq
  684. * policy, or by cpu specific default values determined through
  685. * experimentation.
  686. */
  687. max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits->max_perf));
  688. *max = clamp_t(int, max_perf_adj,
  689. cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
  690. min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits->min_perf));
  691. *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
  692. }
  693. static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force)
  694. {
  695. int max_perf, min_perf;
  696. if (force) {
  697. update_turbo_state();
  698. intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
  699. pstate = clamp_t(int, pstate, min_perf, max_perf);
  700. if (pstate == cpu->pstate.current_pstate)
  701. return;
  702. }
  703. trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
  704. cpu->pstate.current_pstate = pstate;
  705. pstate_funcs.set(cpu, pstate);
  706. }
  707. static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
  708. {
  709. cpu->pstate.min_pstate = pstate_funcs.get_min();
  710. cpu->pstate.max_pstate = pstate_funcs.get_max();
  711. cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
  712. cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
  713. cpu->pstate.scaling = pstate_funcs.get_scaling();
  714. if (pstate_funcs.get_vid)
  715. pstate_funcs.get_vid(cpu);
  716. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
  717. }
  718. static inline void intel_pstate_calc_busy(struct cpudata *cpu)
  719. {
  720. struct sample *sample = &cpu->sample;
  721. int64_t core_pct;
  722. core_pct = int_tofp(sample->aperf) * int_tofp(100);
  723. core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
  724. sample->freq = fp_toint(
  725. mul_fp(int_tofp(
  726. cpu->pstate.max_pstate_physical *
  727. cpu->pstate.scaling / 100),
  728. core_pct));
  729. sample->core_pct_busy = (int32_t)core_pct;
  730. }
  731. static inline void intel_pstate_sample(struct cpudata *cpu)
  732. {
  733. u64 aperf, mperf;
  734. unsigned long flags;
  735. u64 tsc;
  736. local_irq_save(flags);
  737. rdmsrl(MSR_IA32_APERF, aperf);
  738. rdmsrl(MSR_IA32_MPERF, mperf);
  739. if (cpu->prev_mperf == mperf) {
  740. local_irq_restore(flags);
  741. return;
  742. }
  743. tsc = rdtsc();
  744. local_irq_restore(flags);
  745. cpu->last_sample_time = cpu->sample.time;
  746. cpu->sample.time = ktime_get();
  747. cpu->sample.aperf = aperf;
  748. cpu->sample.mperf = mperf;
  749. cpu->sample.tsc = tsc;
  750. cpu->sample.aperf -= cpu->prev_aperf;
  751. cpu->sample.mperf -= cpu->prev_mperf;
  752. cpu->sample.tsc -= cpu->prev_tsc;
  753. intel_pstate_calc_busy(cpu);
  754. cpu->prev_aperf = aperf;
  755. cpu->prev_mperf = mperf;
  756. cpu->prev_tsc = tsc;
  757. }
  758. static inline void intel_hwp_set_sample_time(struct cpudata *cpu)
  759. {
  760. int delay;
  761. delay = msecs_to_jiffies(50);
  762. mod_timer_pinned(&cpu->timer, jiffies + delay);
  763. }
  764. static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
  765. {
  766. int delay;
  767. delay = msecs_to_jiffies(pid_params.sample_rate_ms);
  768. mod_timer_pinned(&cpu->timer, jiffies + delay);
  769. }
  770. static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
  771. {
  772. int32_t core_busy, max_pstate, current_pstate, sample_ratio;
  773. s64 duration_us;
  774. u32 sample_time;
  775. /*
  776. * core_busy is the ratio of actual performance to max
  777. * max_pstate is the max non turbo pstate available
  778. * current_pstate was the pstate that was requested during
  779. * the last sample period.
  780. *
  781. * We normalize core_busy, which was our actual percent
  782. * performance to what we requested during the last sample
  783. * period. The result will be a percentage of busy at a
  784. * specified pstate.
  785. */
  786. core_busy = cpu->sample.core_pct_busy;
  787. max_pstate = int_tofp(cpu->pstate.max_pstate_physical);
  788. current_pstate = int_tofp(cpu->pstate.current_pstate);
  789. core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
  790. /*
  791. * Since we have a deferred timer, it will not fire unless
  792. * we are in C0. So, determine if the actual elapsed time
  793. * is significantly greater (3x) than our sample interval. If it
  794. * is, then we were idle for a long enough period of time
  795. * to adjust our busyness.
  796. */
  797. sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC;
  798. duration_us = ktime_us_delta(cpu->sample.time,
  799. cpu->last_sample_time);
  800. if (duration_us > sample_time * 3) {
  801. sample_ratio = div_fp(int_tofp(sample_time),
  802. int_tofp(duration_us));
  803. core_busy = mul_fp(core_busy, sample_ratio);
  804. }
  805. return core_busy;
  806. }
  807. static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  808. {
  809. int32_t busy_scaled;
  810. struct _pid *pid;
  811. signed int ctl;
  812. int from;
  813. struct sample *sample;
  814. from = cpu->pstate.current_pstate;
  815. pid = &cpu->pid;
  816. busy_scaled = intel_pstate_get_scaled_busy(cpu);
  817. ctl = pid_calc(pid, busy_scaled);
  818. /* Negative values of ctl increase the pstate and vice versa */
  819. intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl, true);
  820. sample = &cpu->sample;
  821. trace_pstate_sample(fp_toint(sample->core_pct_busy),
  822. fp_toint(busy_scaled),
  823. from,
  824. cpu->pstate.current_pstate,
  825. sample->mperf,
  826. sample->aperf,
  827. sample->tsc,
  828. sample->freq);
  829. }
  830. static void intel_hwp_timer_func(unsigned long __data)
  831. {
  832. struct cpudata *cpu = (struct cpudata *) __data;
  833. intel_pstate_sample(cpu);
  834. intel_hwp_set_sample_time(cpu);
  835. }
  836. static void intel_pstate_timer_func(unsigned long __data)
  837. {
  838. struct cpudata *cpu = (struct cpudata *) __data;
  839. intel_pstate_sample(cpu);
  840. intel_pstate_adjust_busy_pstate(cpu);
  841. intel_pstate_set_sample_time(cpu);
  842. }
  843. #define ICPU(model, policy) \
  844. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
  845. (unsigned long)&policy }
  846. static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
  847. ICPU(0x2a, core_params),
  848. ICPU(0x2d, core_params),
  849. ICPU(0x37, silvermont_params),
  850. ICPU(0x3a, core_params),
  851. ICPU(0x3c, core_params),
  852. ICPU(0x3d, core_params),
  853. ICPU(0x3e, core_params),
  854. ICPU(0x3f, core_params),
  855. ICPU(0x45, core_params),
  856. ICPU(0x46, core_params),
  857. ICPU(0x47, core_params),
  858. ICPU(0x4c, airmont_params),
  859. ICPU(0x4e, core_params),
  860. ICPU(0x4f, core_params),
  861. ICPU(0x5e, core_params),
  862. ICPU(0x56, core_params),
  863. ICPU(0x57, knl_params),
  864. {}
  865. };
  866. MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
  867. static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
  868. ICPU(0x56, core_params),
  869. {}
  870. };
  871. static int intel_pstate_init_cpu(unsigned int cpunum)
  872. {
  873. struct cpudata *cpu;
  874. if (!all_cpu_data[cpunum])
  875. all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
  876. GFP_KERNEL);
  877. if (!all_cpu_data[cpunum])
  878. return -ENOMEM;
  879. cpu = all_cpu_data[cpunum];
  880. cpu->cpu = cpunum;
  881. if (hwp_active)
  882. intel_pstate_hwp_enable(cpu);
  883. intel_pstate_get_cpu_pstates(cpu);
  884. init_timer_deferrable(&cpu->timer);
  885. cpu->timer.data = (unsigned long)cpu;
  886. cpu->timer.expires = jiffies + HZ/100;
  887. if (!hwp_active)
  888. cpu->timer.function = intel_pstate_timer_func;
  889. else
  890. cpu->timer.function = intel_hwp_timer_func;
  891. intel_pstate_busy_pid_reset(cpu);
  892. intel_pstate_sample(cpu);
  893. add_timer_on(&cpu->timer, cpunum);
  894. pr_debug("intel_pstate: controlling: cpu %d\n", cpunum);
  895. return 0;
  896. }
  897. static unsigned int intel_pstate_get(unsigned int cpu_num)
  898. {
  899. struct sample *sample;
  900. struct cpudata *cpu;
  901. cpu = all_cpu_data[cpu_num];
  902. if (!cpu)
  903. return 0;
  904. sample = &cpu->sample;
  905. return sample->freq;
  906. }
  907. static int intel_pstate_set_policy(struct cpufreq_policy *policy)
  908. {
  909. if (!policy->cpuinfo.max_freq)
  910. return -ENODEV;
  911. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE &&
  912. policy->max >= policy->cpuinfo.max_freq) {
  913. pr_debug("intel_pstate: set performance\n");
  914. limits = &performance_limits;
  915. if (hwp_active)
  916. intel_pstate_hwp_set();
  917. return 0;
  918. }
  919. pr_debug("intel_pstate: set powersave\n");
  920. limits = &powersave_limits;
  921. limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
  922. limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
  923. limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
  924. policy->cpuinfo.max_freq);
  925. limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
  926. /* Normalize user input to [min_policy_pct, max_policy_pct] */
  927. limits->min_perf_pct = max(limits->min_policy_pct,
  928. limits->min_sysfs_pct);
  929. limits->min_perf_pct = min(limits->max_policy_pct,
  930. limits->min_perf_pct);
  931. limits->max_perf_pct = min(limits->max_policy_pct,
  932. limits->max_sysfs_pct);
  933. limits->max_perf_pct = max(limits->min_policy_pct,
  934. limits->max_perf_pct);
  935. limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
  936. /* Make sure min_perf_pct <= max_perf_pct */
  937. limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
  938. limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
  939. int_tofp(100));
  940. limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
  941. int_tofp(100));
  942. if (hwp_active)
  943. intel_pstate_hwp_set();
  944. return 0;
  945. }
  946. static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
  947. {
  948. cpufreq_verify_within_cpu_limits(policy);
  949. if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
  950. policy->policy != CPUFREQ_POLICY_PERFORMANCE)
  951. return -EINVAL;
  952. return 0;
  953. }
  954. static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
  955. {
  956. int cpu_num = policy->cpu;
  957. struct cpudata *cpu = all_cpu_data[cpu_num];
  958. pr_debug("intel_pstate: CPU %d exiting\n", cpu_num);
  959. del_timer_sync(&all_cpu_data[cpu_num]->timer);
  960. if (hwp_active)
  961. return;
  962. intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
  963. }
  964. static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
  965. {
  966. struct cpudata *cpu;
  967. int rc;
  968. rc = intel_pstate_init_cpu(policy->cpu);
  969. if (rc)
  970. return rc;
  971. cpu = all_cpu_data[policy->cpu];
  972. if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
  973. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  974. else
  975. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  976. policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
  977. policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  978. /* cpuinfo and default policy values */
  979. policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
  980. policy->cpuinfo.max_freq =
  981. cpu->pstate.turbo_pstate * cpu->pstate.scaling;
  982. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  983. cpumask_set_cpu(policy->cpu, policy->cpus);
  984. return 0;
  985. }
  986. static struct cpufreq_driver intel_pstate_driver = {
  987. .flags = CPUFREQ_CONST_LOOPS,
  988. .verify = intel_pstate_verify_policy,
  989. .setpolicy = intel_pstate_set_policy,
  990. .get = intel_pstate_get,
  991. .init = intel_pstate_cpu_init,
  992. .stop_cpu = intel_pstate_stop_cpu,
  993. .name = "intel_pstate",
  994. };
  995. static int __initdata no_load;
  996. static int __initdata no_hwp;
  997. static int __initdata hwp_only;
  998. static unsigned int force_load;
  999. static int intel_pstate_msrs_not_valid(void)
  1000. {
  1001. if (!pstate_funcs.get_max() ||
  1002. !pstate_funcs.get_min() ||
  1003. !pstate_funcs.get_turbo())
  1004. return -ENODEV;
  1005. return 0;
  1006. }
  1007. static void copy_pid_params(struct pstate_adjust_policy *policy)
  1008. {
  1009. pid_params.sample_rate_ms = policy->sample_rate_ms;
  1010. pid_params.p_gain_pct = policy->p_gain_pct;
  1011. pid_params.i_gain_pct = policy->i_gain_pct;
  1012. pid_params.d_gain_pct = policy->d_gain_pct;
  1013. pid_params.deadband = policy->deadband;
  1014. pid_params.setpoint = policy->setpoint;
  1015. }
  1016. static void copy_cpu_funcs(struct pstate_funcs *funcs)
  1017. {
  1018. pstate_funcs.get_max = funcs->get_max;
  1019. pstate_funcs.get_max_physical = funcs->get_max_physical;
  1020. pstate_funcs.get_min = funcs->get_min;
  1021. pstate_funcs.get_turbo = funcs->get_turbo;
  1022. pstate_funcs.get_scaling = funcs->get_scaling;
  1023. pstate_funcs.set = funcs->set;
  1024. pstate_funcs.get_vid = funcs->get_vid;
  1025. }
  1026. #if IS_ENABLED(CONFIG_ACPI)
  1027. #include <acpi/processor.h>
  1028. static bool intel_pstate_no_acpi_pss(void)
  1029. {
  1030. int i;
  1031. for_each_possible_cpu(i) {
  1032. acpi_status status;
  1033. union acpi_object *pss;
  1034. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1035. struct acpi_processor *pr = per_cpu(processors, i);
  1036. if (!pr)
  1037. continue;
  1038. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  1039. if (ACPI_FAILURE(status))
  1040. continue;
  1041. pss = buffer.pointer;
  1042. if (pss && pss->type == ACPI_TYPE_PACKAGE) {
  1043. kfree(pss);
  1044. return false;
  1045. }
  1046. kfree(pss);
  1047. }
  1048. return true;
  1049. }
  1050. static bool intel_pstate_has_acpi_ppc(void)
  1051. {
  1052. int i;
  1053. for_each_possible_cpu(i) {
  1054. struct acpi_processor *pr = per_cpu(processors, i);
  1055. if (!pr)
  1056. continue;
  1057. if (acpi_has_method(pr->handle, "_PPC"))
  1058. return true;
  1059. }
  1060. return false;
  1061. }
  1062. enum {
  1063. PSS,
  1064. PPC,
  1065. };
  1066. struct hw_vendor_info {
  1067. u16 valid;
  1068. char oem_id[ACPI_OEM_ID_SIZE];
  1069. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
  1070. int oem_pwr_table;
  1071. };
  1072. /* Hardware vendor-specific info that has its own power management modes */
  1073. static struct hw_vendor_info vendor_info[] = {
  1074. {1, "HP ", "ProLiant", PSS},
  1075. {1, "ORACLE", "X4-2 ", PPC},
  1076. {1, "ORACLE", "X4-2L ", PPC},
  1077. {1, "ORACLE", "X4-2B ", PPC},
  1078. {1, "ORACLE", "X3-2 ", PPC},
  1079. {1, "ORACLE", "X3-2L ", PPC},
  1080. {1, "ORACLE", "X3-2B ", PPC},
  1081. {1, "ORACLE", "X4470M2 ", PPC},
  1082. {1, "ORACLE", "X4270M3 ", PPC},
  1083. {1, "ORACLE", "X4270M2 ", PPC},
  1084. {1, "ORACLE", "X4170M2 ", PPC},
  1085. {1, "ORACLE", "X4170 M3", PPC},
  1086. {1, "ORACLE", "X4275 M3", PPC},
  1087. {1, "ORACLE", "X6-2 ", PPC},
  1088. {1, "ORACLE", "Sudbury ", PPC},
  1089. {0, "", ""},
  1090. };
  1091. static bool intel_pstate_platform_pwr_mgmt_exists(void)
  1092. {
  1093. struct acpi_table_header hdr;
  1094. struct hw_vendor_info *v_info;
  1095. const struct x86_cpu_id *id;
  1096. u64 misc_pwr;
  1097. id = x86_match_cpu(intel_pstate_cpu_oob_ids);
  1098. if (id) {
  1099. rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
  1100. if ( misc_pwr & (1 << 8))
  1101. return true;
  1102. }
  1103. if (acpi_disabled ||
  1104. ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
  1105. return false;
  1106. for (v_info = vendor_info; v_info->valid; v_info++) {
  1107. if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
  1108. !strncmp(hdr.oem_table_id, v_info->oem_table_id,
  1109. ACPI_OEM_TABLE_ID_SIZE))
  1110. switch (v_info->oem_pwr_table) {
  1111. case PSS:
  1112. return intel_pstate_no_acpi_pss();
  1113. case PPC:
  1114. return intel_pstate_has_acpi_ppc() &&
  1115. (!force_load);
  1116. }
  1117. }
  1118. return false;
  1119. }
  1120. #else /* CONFIG_ACPI not enabled */
  1121. static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
  1122. static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
  1123. #endif /* CONFIG_ACPI */
  1124. static const struct x86_cpu_id hwp_support_ids[] __initconst = {
  1125. { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
  1126. {}
  1127. };
  1128. static int __init intel_pstate_init(void)
  1129. {
  1130. int cpu, rc = 0;
  1131. const struct x86_cpu_id *id;
  1132. struct cpu_defaults *cpu_def;
  1133. if (no_load)
  1134. return -ENODEV;
  1135. if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
  1136. copy_cpu_funcs(&core_params.funcs);
  1137. hwp_active++;
  1138. goto hwp_cpu_matched;
  1139. }
  1140. id = x86_match_cpu(intel_pstate_cpu_ids);
  1141. if (!id)
  1142. return -ENODEV;
  1143. cpu_def = (struct cpu_defaults *)id->driver_data;
  1144. copy_pid_params(&cpu_def->pid_policy);
  1145. copy_cpu_funcs(&cpu_def->funcs);
  1146. if (intel_pstate_msrs_not_valid())
  1147. return -ENODEV;
  1148. hwp_cpu_matched:
  1149. /*
  1150. * The Intel pstate driver will be ignored if the platform
  1151. * firmware has its own power management modes.
  1152. */
  1153. if (intel_pstate_platform_pwr_mgmt_exists())
  1154. return -ENODEV;
  1155. pr_info("Intel P-state driver initializing.\n");
  1156. all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
  1157. if (!all_cpu_data)
  1158. return -ENOMEM;
  1159. if (!hwp_active && hwp_only)
  1160. goto out;
  1161. rc = cpufreq_register_driver(&intel_pstate_driver);
  1162. if (rc)
  1163. goto out;
  1164. intel_pstate_debug_expose_params();
  1165. intel_pstate_sysfs_expose_params();
  1166. if (hwp_active)
  1167. pr_info("intel_pstate: HWP enabled\n");
  1168. return rc;
  1169. out:
  1170. get_online_cpus();
  1171. for_each_online_cpu(cpu) {
  1172. if (all_cpu_data[cpu]) {
  1173. del_timer_sync(&all_cpu_data[cpu]->timer);
  1174. kfree(all_cpu_data[cpu]);
  1175. }
  1176. }
  1177. put_online_cpus();
  1178. vfree(all_cpu_data);
  1179. return -ENODEV;
  1180. }
  1181. device_initcall(intel_pstate_init);
  1182. static int __init intel_pstate_setup(char *str)
  1183. {
  1184. if (!str)
  1185. return -EINVAL;
  1186. if (!strcmp(str, "disable"))
  1187. no_load = 1;
  1188. if (!strcmp(str, "no_hwp")) {
  1189. pr_info("intel_pstate: HWP disabled\n");
  1190. no_hwp = 1;
  1191. }
  1192. if (!strcmp(str, "force"))
  1193. force_load = 1;
  1194. if (!strcmp(str, "hwp_only"))
  1195. hwp_only = 1;
  1196. return 0;
  1197. }
  1198. early_param("intel_pstate", intel_pstate_setup);
  1199. MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
  1200. MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
  1201. MODULE_LICENSE("GPL");