power.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /* Intel P-state and deep C-state management.
  2. *
  3. * Nehalem/Westmere (the X201) get the full race-to-idle P-state governor:
  4. * the module writes IA32_PERF_CTL (lowest ratio for idle CPUs, maximum
  5. * non-turbo ratio for a CPU that is running something, linear in between for
  6. * mixed windows) and idle.c uses C1E/C3/C6 MWAIT hints.
  7. *
  8. * Braswell/Airmont (the HP Chromebook, family 6 model 0x4C) reads the Atom
  9. * ratio/VID MSRs and MSR_FSB_FREQ, writes IA32_PERF_CTL with the pstate in
  10. * 15:8 and the Linux-interpolated VID in 7:0, and runs the same race-to-idle
  11. * governor. It enters at the maximum non-turbo pstate (pstate 20 = 1600 MHz
  12. * on the N3060) and drops to pstate 6 (480 MHz) when idle; the turbo pstate
  13. * is detected but not requested. The package/power-SKU and auto-demotion
  14. * MSRs are deliberately not touched (coreboot configures them, and an
  15. * unsupported rdmsr before the IDT exists triple-faults). APERF/MPERF are
  16. * used only when CPUID.06H:ECX[0] says they exist.
  17. *
  18. * The module only touches documented MSRs and is gated on the model list;
  19. * unknown CPUs keep the old HLT behaviour.
  20. */
  21. #include "power.h"
  22. #include "bootcon.h"
  23. #include "cpu.h"
  24. #include "idle.h"
  25. #include "io.h"
  26. #include "sched.h"
  27. #include "serial.h"
  28. #define MSR_IA32_PERF_STATUS 0x198
  29. #define MSR_IA32_PERF_CTL 0x199
  30. #define MSR_PLATFORM_INFO 0x0CE
  31. #define MSR_FSB_FREQ 0x0CD
  32. #define MSR_MISC_ENABLE 0x1A0
  33. #define MSR_MISC_PWR_MGMT 0x1AA
  34. #define MSR_POWER_CTL 0x1FC
  35. #define MSR_PKG_CST_CONFIG 0x0E2
  36. #define MSR_PKG_C3_RESIDENCY 0x3F8
  37. #define MSR_PKG_C6_RESIDENCY 0x3F9
  38. #define MSR_CORE_C3_RESIDENCY 0x3FC
  39. #define MSR_CORE_C6_RESIDENCY 0x3FD
  40. #define MSR_APERF 0x0E8
  41. #define MSR_MPERF 0x0E7
  42. #define MSR_TSC 0x010
  43. #define MSR_ATOM_CORE_RATIOS 0x66A
  44. #define MSR_ATOM_CORE_VIDS 0x66B
  45. #define MSR_ATOM_CORE_TURBO_RATIOS 0x66C
  46. #define MSR_ATOM_CORE_TURBO_VIDS 0x66D
  47. #define MSR_PM_ENABLE 0x770
  48. #define MSR_IA32_HWP_CAPABILITIES 0x771
  49. #define MSR_IA32_HWP_REQUEST 0x774
  50. /* P-state ratios are the bus-ratio multipliers (133.33 MHz on Arrandale). */
  51. #define BCLK_MHZ 133u
  52. #define PLAT_NEHALEM 1
  53. #define PLAT_AIRMONT 2
  54. #define PLAT_KABYLAKE 3
  55. /* Airmont MWAIT C6N hint from Linux's intel_idle cht_cstates table. */
  56. #define MWAIT_C6N 0x58
  57. struct power_cpu {
  58. /* Governor state (100 ms windows). */
  59. unsigned int div; /* ms in this window */
  60. unsigned int busy_ms; /* ms this CPU ran a non-idle proc */
  61. unsigned int streak_ms; /* consecutive busy ms */
  62. unsigned int req; /* last ratio written */
  63. /* Once-a-second statistics. */
  64. unsigned int stat_div;
  65. int have_last;
  66. unsigned long long aperf, mperf, c3, c6, tsc;
  67. unsigned int mhz;
  68. unsigned int ratio;
  69. unsigned int status; /* raw IA32_PERF_STATUS[15:0] */
  70. unsigned int pc3, pc6; /* permille */
  71. };
  72. static struct power_cpu pcs[MAX_CPUS];
  73. static int platform;
  74. static int pstate_writes;
  75. static int supported;
  76. static int has_arat;
  77. static int has_aperf;
  78. static int eist_on;
  79. static int turbo_disabled;
  80. static int c1e_on;
  81. static unsigned int cst_limit;
  82. static int cst_locked;
  83. static unsigned int min_ratio = 6;
  84. static unsigned int max_ratio;
  85. static unsigned int ratio_shift;
  86. static unsigned int nominal_mhz;
  87. static int cpufreq_mode = POWER_CPUFREQ_AUTO;
  88. static unsigned int fixed_ratio;
  89. static unsigned long long pkg_c3_last, pkg_c6_last;
  90. static unsigned int pkg_pc3, pkg_pc6;
  91. static int pkg_have_last;
  92. /* Airmont probe values (kept for the `power` output and the follow-up that
  93. * enables the governor). */
  94. static unsigned int fsb_khz;
  95. static unsigned int turbo_ratio;
  96. static unsigned int turbo_vid;
  97. static unsigned int atom_vid_min;
  98. static unsigned int atom_vid_max;
  99. static int atom_vid_valid;
  100. static unsigned int c6n_ok;
  101. static int c6n_enabled;
  102. static unsigned int plat_info_lo, plat_info_hi;
  103. static int hwp; /* IA32_PM_ENABLE bit 0 */
  104. static unsigned int hwp_epp = 128; /* balance_performance */
  105. static void cpuid_leaf(unsigned int leaf, unsigned int *a, unsigned int *b,
  106. unsigned int *c, unsigned int *d)
  107. {
  108. __asm__ volatile ("cpuid"
  109. : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
  110. : "a"(leaf), "c"(0));
  111. }
  112. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  113. {
  114. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  115. }
  116. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  117. {
  118. __asm__ volatile ("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));
  119. }
  120. static unsigned long long rdmsr64(unsigned int msr)
  121. {
  122. unsigned int lo, hi;
  123. rdmsr(msr, &lo, &hi);
  124. return ((unsigned long long)hi << 32) | lo;
  125. }
  126. /* Model allowlist. Nehalem/Westmere implement the P-state/C-state MSRs
  127. * used here; Braswell is Airmont family 6 model 0x4C. Bonnell
  128. * (0x1C/0x1D) is deliberately absent. */
  129. static int power_platform(unsigned int model)
  130. {
  131. switch (model) {
  132. case 0x1A: /* Nehalem */
  133. case 0x1E: /* Lynnfield/Clarksfield */
  134. case 0x1F: /* Nehalem-EX */
  135. case 0x25: /* Arrandale/Clarkdale (X201 i5 M 520) */
  136. case 0x2C: /* Westmere-EP */
  137. case 0x2E: /* Nehalem-EX */
  138. case 0x2F: /* Westmere-EX */
  139. return PLAT_NEHALEM;
  140. case 0x4C: /* Airmont: Braswell / Cherry Trail */
  141. return PLAT_AIRMONT;
  142. case 0x5E: /* Skylake-U */
  143. case 0x8E: /* Kaby Lake-U (Yoga 720) */
  144. case 0x9E: /* Coffee Lake */
  145. return PLAT_KABYLAKE;
  146. default:
  147. return 0;
  148. }
  149. }
  150. /* Airmont P-state VID for a target pstate. Linux intel_pstate interpolates
  151. * linearly between the min/max VID from MSR_ATOM_CORE_VIDS; the P-unit uses
  152. * the low byte of IA32_PERF_CTL together with the ratio in 15:8. */
  153. static unsigned int atom_vid_for(unsigned int ratio)
  154. {
  155. unsigned int span = max_ratio - min_ratio;
  156. unsigned int v;
  157. if (!atom_vid_valid || !span)
  158. return 0;
  159. if (ratio <= min_ratio)
  160. return atom_vid_min & 0x7F;
  161. if (ratio >= max_ratio)
  162. return atom_vid_max & 0x7F;
  163. v = atom_vid_min +
  164. ((ratio - min_ratio) * (atom_vid_max - atom_vid_min) +
  165. span / 2) / span;
  166. return v & 0x7F;
  167. }
  168. /* Write one target ratio. Nehalem/Westmere use bits 15:8 of
  169. * IA32_PERF_CTL for the target ratio; the low byte is reserved and written
  170. * as zero (coreboot does the same). Airmont takes the ratio in 15:8 and the
  171. * interpolated VID in 7:0. */
  172. static void power_write_ratio(unsigned int ratio)
  173. {
  174. unsigned int lo, hi;
  175. if (ratio < min_ratio)
  176. ratio = min_ratio;
  177. if (ratio > max_ratio)
  178. ratio = max_ratio;
  179. if (platform == PLAT_KABYLAKE) {
  180. if (hwp) {
  181. unsigned int req = (min_ratio & 0xFFu) |
  182. ((ratio & 0xFFu) << 8) |
  183. ((ratio & 0xFFu) << 16) |
  184. ((hwp_epp & 0xFFu) << 24);
  185. wrmsr(MSR_IA32_HWP_REQUEST, req, 0);
  186. return;
  187. }
  188. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  189. lo = (lo & 0xFFFF0000u) | (ratio << 8);
  190. wrmsr(MSR_IA32_PERF_CTL, lo, hi);
  191. return;
  192. }
  193. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  194. if (platform == PLAT_AIRMONT) {
  195. lo = (lo & 0xFFFF0000u) | ((ratio & 0xFF) << 8) |
  196. atom_vid_for(ratio);
  197. } else if (ratio_shift) {
  198. /* Bits 15:8 carry the ratio and 7:0 are reserved (the old VID
  199. * slot); coreboot writes them as zero. Keeping the BIOS VID
  200. * byte here made the request silently ineffective. */
  201. lo = (lo & 0xFFFF0000u) | (ratio << 8);
  202. } else {
  203. lo = (lo & ~0xFFu) | ratio;
  204. }
  205. wrmsr(MSR_IA32_PERF_CTL, lo, hi);
  206. }
  207. static void power_apply(struct power_cpu *p, unsigned int ratio)
  208. {
  209. if (ratio < min_ratio)
  210. ratio = min_ratio;
  211. if (ratio > max_ratio)
  212. ratio = max_ratio;
  213. if (ratio == p->req)
  214. return;
  215. power_write_ratio(ratio);
  216. p->req = ratio;
  217. }
  218. /* Airmont FSB frequencies in kHz, SDM Table 35-10 (index in MSR_FSB_FREQ). */
  219. static unsigned int airmont_fsb(unsigned int index)
  220. {
  221. static const unsigned int table[9] = {
  222. 83300, 100000, 133300, 116700, 80000,
  223. 93300, 90000, 88900, 87500
  224. };
  225. if (index >= sizeof(table) / sizeof(table[0]))
  226. return 100000;
  227. return table[index];
  228. }
  229. static void power_init_airmont(unsigned int max_leaf)
  230. {
  231. unsigned int lo, hi, vlo, vhi, tlo, thi, slo, shi;
  232. unsigned int misc_lo, misc_hi;
  233. unsigned int idx, r, i;
  234. bootcon_puts("power: airmont init\n");
  235. rdmsr(MSR_PLATFORM_INFO, &plat_info_lo, &plat_info_hi);
  236. rdmsr(MSR_ATOM_CORE_RATIOS, &lo, &hi);
  237. min_ratio = (lo >> 8) & 0x7F;
  238. max_ratio = (lo >> 16) & 0x7F;
  239. /* If the Atom-specific MSR is not populated, fall back to the
  240. * architectural PLATFORM_INFO fields. */
  241. if (max_ratio < 5 || max_ratio > 60 || min_ratio < 3 ||
  242. min_ratio > max_ratio) {
  243. max_ratio = (plat_info_lo >> 8) & 0xFF;
  244. min_ratio = (plat_info_hi >> 8) & 0xFF;
  245. }
  246. if (max_ratio < 5 || max_ratio > 60 || min_ratio < 3 ||
  247. min_ratio > max_ratio) {
  248. serial_puts(SERIAL_COM1,
  249. "power: implausible Atom ratios, disabled\n");
  250. return;
  251. }
  252. bootcon_puts("power: ratios ok\n");
  253. rdmsr(MSR_FSB_FREQ, &lo, &hi);
  254. idx = lo & 0xF;
  255. fsb_khz = airmont_fsb(idx);
  256. nominal_mhz = (max_ratio * fsb_khz + 500) / 1000;
  257. if (!nominal_mhz) {
  258. serial_puts(SERIAL_COM1, "power: zero nominal frequency, disabled\n");
  259. return;
  260. }
  261. rdmsr(MSR_ATOM_CORE_VIDS, &vlo, &vhi);
  262. atom_vid_min = (vlo >> 8) & 0x7F;
  263. atom_vid_max = (vlo >> 16) & 0x7F;
  264. atom_vid_valid = (max_ratio > min_ratio &&
  265. atom_vid_min <= atom_vid_max);
  266. rdmsr(MSR_ATOM_CORE_TURBO_RATIOS, &tlo, &thi);
  267. turbo_ratio = tlo & 0x7F;
  268. rdmsr(MSR_ATOM_CORE_TURBO_VIDS, &slo, &shi);
  269. turbo_vid = slo & 0x7F;
  270. rdmsr(MSR_MISC_ENABLE, &misc_lo, &misc_hi);
  271. eist_on = (misc_lo >> 16) & 1;
  272. turbo_disabled = (misc_hi >> 6) & 1;
  273. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  274. r = (lo >> 8) & 0xFF;
  275. if (r >= min_ratio && r <= max_ratio) {
  276. pcs[0].ratio = r;
  277. pcs[0].req = r;
  278. } else {
  279. pcs[0].ratio = max_ratio;
  280. pcs[0].req = max_ratio;
  281. }
  282. pcs[0].status = lo & 0xFFFF;
  283. bootcon_puts("power: msrs ok\n");
  284. has_arat = 0;
  285. c6n_ok = 0;
  286. if (max_leaf >= 5) {
  287. unsigned int a, b, c, d;
  288. cpuid_leaf(5, &a, &b, &c, &d);
  289. c6n_ok = ((d >> 24) & 0xF) ? 1 : 0;
  290. }
  291. if (max_leaf >= 6) {
  292. unsigned int a, b, c, d;
  293. cpuid_leaf(6, &a, &b, &c, &d);
  294. has_arat = (a & 4u) ? 1 : 0;
  295. has_aperf = (c & 1u) ? 1 : 0;
  296. }
  297. c1e_on = 0;
  298. cst_limit = 0;
  299. cst_locked = 0;
  300. pkg_have_last = 0;
  301. /* The governor starts in auto: enter at max non-turbo, then drop to
  302. * pstate 6 after the first idle window. C6N is enabled when CPUID
  303. * advertises the C6 substates. */
  304. cpufreq_mode = POWER_CPUFREQ_AUTO;
  305. supported = 1;
  306. pstate_writes = 1;
  307. for (i = 0; i < MAX_CPUS; i++) {
  308. pcs[i].req = 0;
  309. pcs[i].ratio = max_ratio;
  310. }
  311. c6n_enabled = c6n_ok ? 1 : 0;
  312. if (has_aperf) {
  313. pcs[0].aperf = rdmsr64(MSR_APERF);
  314. pcs[0].mperf = rdmsr64(MSR_MPERF);
  315. }
  316. pcs[0].have_last = 1;
  317. bootcon_puts("power: apis ok\n");
  318. /* Enter at the maximum non-turbo pstate; the race-to-idle governor
  319. * drops to the minimum after the first idle window. */
  320. pcs[0].req = max_ratio;
  321. power_write_ratio(max_ratio);
  322. bootcon_puts("power: pstate writes enabled\n");
  323. if (c6n_enabled)
  324. bootcon_puts("power: c6n enabled\n");
  325. serial_puts(SERIAL_COM1,
  326. "power: Airmont/Braswell, P-states auto");
  327. if (c6n_enabled)
  328. serial_puts(SERIAL_COM1, ", C6N enabled");
  329. else
  330. serial_puts(SERIAL_COM1, ", C6N disabled");
  331. serial_puts(SERIAL_COM1, "\n");
  332. serial_puts(SERIAL_COM1, "power: pstates ");
  333. serial_putdec(SERIAL_COM1, min_ratio);
  334. serial_puts(SERIAL_COM1, "..");
  335. serial_putdec(SERIAL_COM1, max_ratio);
  336. serial_puts(SERIAL_COM1, ", fsb ");
  337. serial_putdec(SERIAL_COM1, fsb_khz);
  338. serial_puts(SERIAL_COM1, " kHz, nominal ");
  339. serial_putdec(SERIAL_COM1, nominal_mhz);
  340. serial_puts(SERIAL_COM1, " MHz, vid ");
  341. serial_puthex(SERIAL_COM1, atom_vid_min);
  342. serial_puts(SERIAL_COM1, "..");
  343. serial_puthex(SERIAL_COM1, atom_vid_max);
  344. serial_puts(SERIAL_COM1, atom_vid_valid ? "" : " (invalid)");
  345. serial_puts(SERIAL_COM1, "\npower: turbo p");
  346. serial_putdec(SERIAL_COM1, turbo_ratio);
  347. serial_puts(SERIAL_COM1, " vid ");
  348. serial_puthex(SERIAL_COM1, turbo_vid);
  349. serial_puts(SERIAL_COM1, ", eist ");
  350. serial_puts(SERIAL_COM1, eist_on ? "on" : "off");
  351. serial_puts(SERIAL_COM1, ", turbo ");
  352. serial_puts(SERIAL_COM1, turbo_disabled ? "disabled" : "enabled");
  353. serial_puts(SERIAL_COM1, ", arat ");
  354. serial_puts(SERIAL_COM1, has_arat ? "yes" : "no");
  355. serial_puts(SERIAL_COM1, ", c6n ");
  356. serial_puts(SERIAL_COM1, c6n_ok ? "yes" : "no");
  357. serial_puts(SERIAL_COM1, "\npower: platform info ");
  358. serial_puthex(SERIAL_COM1, plat_info_hi);
  359. serial_puthex(SERIAL_COM1, plat_info_lo);
  360. serial_puts(SERIAL_COM1, ", perf status ");
  361. serial_puthex(SERIAL_COM1, pcs[0].status);
  362. serial_puts(SERIAL_COM1, ", perf ctl ");
  363. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  364. serial_puthex(SERIAL_COM1, lo);
  365. serial_puts(SERIAL_COM1, "\npower: misc enable ");
  366. serial_puthex(SERIAL_COM1, misc_lo);
  367. serial_puts(SERIAL_COM1, "\n");
  368. }
  369. static void power_init_kabylake(unsigned int max_leaf)
  370. {
  371. unsigned int lo, hi, a, b, c, d;
  372. unsigned int caps, req, misc_lo, misc_hi;
  373. unsigned int highest, guaranteed, lowest;
  374. bootcon_puts("power: kaby lake init\n");
  375. rdmsr(MSR_PLATFORM_INFO, &plat_info_lo, &plat_info_hi);
  376. max_ratio = (plat_info_lo >> 8) & 0xFFu;
  377. min_ratio = (plat_info_hi >> 8) & 0xFFu;
  378. if (max_ratio < 5 || max_ratio > 60 || min_ratio < 3 ||
  379. min_ratio > max_ratio) {
  380. serial_puts(SERIAL_COM1,
  381. "power: implausible platform info, disabled\n");
  382. return;
  383. }
  384. nominal_mhz = max_ratio * 100u;
  385. ratio_shift = 8;
  386. turbo_ratio = max_ratio;
  387. hwp = 0;
  388. hwp_epp = 128;
  389. /* HWP/Speed Shift: IA32_HWP_CAPABILITIES reports highest (turbo),
  390. * guaranteed (max non-turbo), most efficient and lowest ratios. The
  391. * governor uses the guaranteed..lowest range and leaves turbo
  392. * unrequested, like the other platforms. */
  393. rdmsr(MSR_PM_ENABLE, &lo, &hi);
  394. if (lo & 1u) {
  395. hwp = 1;
  396. rdmsr(MSR_IA32_HWP_CAPABILITIES, &caps, &hi);
  397. highest = caps & 0xFFu;
  398. guaranteed = (caps >> 8) & 0xFFu;
  399. lowest = (caps >> 24) & 0xFFu;
  400. if (highest >= max_ratio && highest <= 60)
  401. turbo_ratio = highest;
  402. if (guaranteed >= 5 && guaranteed <= 60)
  403. max_ratio = guaranteed;
  404. if (lowest >= 3 && lowest <= max_ratio)
  405. min_ratio = lowest;
  406. rdmsr(MSR_IA32_HWP_REQUEST, &req, &hi);
  407. hwp_epp = (req >> 24) & 0xFFu;
  408. }
  409. rdmsr(MSR_MISC_ENABLE, &misc_lo, &misc_hi);
  410. eist_on = (misc_lo >> 16) & 1;
  411. turbo_disabled = (misc_hi >> 6) & 1;
  412. if (max_leaf >= 6) {
  413. cpuid_leaf(6, &a, &b, &c, &d);
  414. has_arat = (a & 4u) ? 1 : 0;
  415. }
  416. supported = 1;
  417. pstate_writes = 1;
  418. has_aperf = 1;
  419. pcs[0].req = max_ratio;
  420. power_write_ratio(max_ratio);
  421. pcs[0].aperf = rdmsr64(MSR_APERF);
  422. pcs[0].mperf = rdmsr64(MSR_MPERF);
  423. pcs[0].have_last = 1;
  424. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  425. bootcon_puts("power: kaby lake ready\n");
  426. serial_puts(SERIAL_COM1, "power: Kaby Lake, pstates ");
  427. serial_putdec(SERIAL_COM1, min_ratio);
  428. serial_puts(SERIAL_COM1, "..");
  429. serial_putdec(SERIAL_COM1, max_ratio);
  430. serial_puts(SERIAL_COM1, " (");
  431. serial_putdec(SERIAL_COM1, nominal_mhz);
  432. serial_puts(SERIAL_COM1, " MHz), turbo p");
  433. serial_putdec(SERIAL_COM1, turbo_ratio);
  434. serial_puts(SERIAL_COM1, ", hwp ");
  435. serial_puts(SERIAL_COM1, hwp ? "on" : "off");
  436. serial_puts(SERIAL_COM1, ", epp ");
  437. serial_putdec(SERIAL_COM1, hwp_epp);
  438. serial_puts(SERIAL_COM1, ", eist ");
  439. serial_puts(SERIAL_COM1, eist_on ? "on" : "off");
  440. serial_puts(SERIAL_COM1, ", turbo ");
  441. serial_puts(SERIAL_COM1, turbo_disabled ? "disabled" : "enabled");
  442. serial_puts(SERIAL_COM1, ", arat ");
  443. serial_puts(SERIAL_COM1, has_arat ? "yes" : "no");
  444. serial_puts(SERIAL_COM1, ", perf status ");
  445. serial_puthex(SERIAL_COM1, lo);
  446. serial_puts(SERIAL_COM1, "\n");
  447. }
  448. void power_init(void)
  449. {
  450. unsigned int max_leaf, a, b, c, d;
  451. unsigned int family, model;
  452. unsigned int lo, hi;
  453. supported = 0;
  454. pstate_writes = 0;
  455. platform = 0;
  456. has_arat = 0;
  457. has_aperf = 0;
  458. eist_on = 0;
  459. turbo_disabled = 0;
  460. c1e_on = 0;
  461. cst_limit = 0;
  462. cst_locked = 0;
  463. min_ratio = 6;
  464. max_ratio = 0;
  465. ratio_shift = 0;
  466. nominal_mhz = 0;
  467. cpufreq_mode = POWER_CPUFREQ_AUTO;
  468. fixed_ratio = 0;
  469. fsb_khz = 0;
  470. turbo_ratio = 0;
  471. turbo_vid = 0;
  472. atom_vid_min = 0;
  473. atom_vid_max = 0;
  474. atom_vid_valid = 0;
  475. c6n_ok = 0;
  476. c6n_enabled = 0;
  477. plat_info_lo = 0;
  478. plat_info_hi = 0;
  479. pkg_have_last = 0;
  480. pkg_pc3 = 0;
  481. pkg_pc6 = 0;
  482. if (!cpu_has_cpuid()) {
  483. serial_puts(SERIAL_COM1, "power: no CPUID, disabled\n");
  484. return;
  485. }
  486. cpuid_leaf(0, &max_leaf, &b, &c, &d);
  487. if (max_leaf < 1) {
  488. serial_puts(SERIAL_COM1, "power: no CPUID leaf 1\n");
  489. return;
  490. }
  491. cpuid_leaf(1, &a, &b, &c, &d);
  492. family = (a >> 8) & 0xF;
  493. model = (a >> 4) & 0xF;
  494. if (family == 0xF)
  495. family += (a >> 20) & 0xFF;
  496. if (family == 0x6 || family == 0xF)
  497. model += ((a >> 16) & 0xF) << 4;
  498. platform = (family == 0x6) ? power_platform(model) : 0;
  499. if (!platform) {
  500. serial_puts(SERIAL_COM1, "power: CPU family ");
  501. serial_putdec(SERIAL_COM1, family);
  502. serial_puts(SERIAL_COM1, " model ");
  503. serial_putdec(SERIAL_COM1, model);
  504. serial_puts(SERIAL_COM1, ", no supported PM\n");
  505. return;
  506. }
  507. if (platform == PLAT_AIRMONT) {
  508. power_init_airmont(max_leaf);
  509. return;
  510. }
  511. if (platform == PLAT_KABYLAKE) {
  512. power_init_kabylake(max_leaf);
  513. return;
  514. }
  515. rdmsr(MSR_PLATFORM_INFO, &lo, &hi);
  516. max_ratio = (lo >> 8) & 0xFF;
  517. min_ratio = (hi >> 8) & 0xFF;
  518. if (max_ratio < 5 || max_ratio > 60 || min_ratio < 3 ||
  519. min_ratio > max_ratio) {
  520. serial_puts(SERIAL_COM1, "power: implausible platform info, disabled\n");
  521. return;
  522. }
  523. nominal_mhz = max_ratio * BCLK_MHZ + max_ratio / 3;
  524. /* Nehalem/Westmere keep the target ratio in bits 15:8 of
  525. * IA32_PERF_CTL/STATUS; the low byte is the (now unused) VID field.
  526. * coreboot's set_max_ratio writes (ratio << 8) for this family, and
  527. * reading the low byte on the X201 gives VID-looking values (0x11,
  528. * 0x18) that would otherwise masquerade as ratios. */
  529. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  530. ratio_shift = 8;
  531. if (max_leaf >= 6) {
  532. cpuid_leaf(6, &a, &b, &c, &d);
  533. has_arat = (a & 4u) ? 1 : 0;
  534. }
  535. rdmsr(MSR_MISC_ENABLE, &lo, &hi);
  536. eist_on = (lo >> 16) & 1;
  537. turbo_disabled = (hi >> 6) & 1;
  538. if (!eist_on) {
  539. wrmsr(MSR_MISC_ENABLE, lo | (1u << 16), hi);
  540. eist_on = 1;
  541. }
  542. rdmsr(MSR_POWER_CTL, &lo, &hi);
  543. c1e_on = (lo >> 1) & 1;
  544. if (!c1e_on) {
  545. wrmsr(MSR_POWER_CTL, lo | (1u << 1), hi);
  546. c1e_on = 1;
  547. }
  548. rdmsr(MSR_PKG_CST_CONFIG, &lo, &hi);
  549. cst_limit = lo & 7u;
  550. cst_locked = (lo >> 15) & 1;
  551. if (!cst_locked && cst_limit < 3) {
  552. wrmsr(MSR_PKG_CST_CONFIG, (lo & ~7u) | 3u, hi);
  553. cst_limit = 3;
  554. }
  555. rdmsr(MSR_MISC_PWR_MGMT, &lo, &hi);
  556. supported = 1;
  557. pstate_writes = 1;
  558. has_aperf = 1;
  559. /* Start at the maximum non-turbo ratio; the governor drops to the
  560. * minimum after the first idle window. */
  561. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  562. serial_puts(SERIAL_COM1, "power: perf status ");
  563. serial_puthex(SERIAL_COM1, lo);
  564. pcs[0].req = max_ratio;
  565. power_write_ratio(max_ratio);
  566. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  567. serial_puts(SERIAL_COM1, ", perf ctl ");
  568. serial_puthex(SERIAL_COM1, lo);
  569. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  570. serial_puts(SERIAL_COM1, ", status now ");
  571. serial_puthex(SERIAL_COM1, lo);
  572. serial_puts(SERIAL_COM1, "\n");
  573. pcs[0].aperf = rdmsr64(MSR_APERF);
  574. pcs[0].mperf = rdmsr64(MSR_MPERF);
  575. pcs[0].c3 = rdmsr64(MSR_CORE_C3_RESIDENCY);
  576. pcs[0].c6 = rdmsr64(MSR_CORE_C6_RESIDENCY);
  577. pcs[0].tsc = rdmsr64(MSR_TSC);
  578. pcs[0].have_last = 1;
  579. pkg_c3_last = rdmsr64(MSR_PKG_C3_RESIDENCY);
  580. pkg_c6_last = rdmsr64(MSR_PKG_C6_RESIDENCY);
  581. pkg_have_last = 1;
  582. serial_puts(SERIAL_COM1, "power: Nehalem/Westmere, ratios ");
  583. serial_putdec(SERIAL_COM1, min_ratio);
  584. serial_puts(SERIAL_COM1, "..");
  585. serial_putdec(SERIAL_COM1, max_ratio);
  586. serial_puts(SERIAL_COM1, " (");
  587. serial_putdec(SERIAL_COM1, nominal_mhz);
  588. serial_puts(SERIAL_COM1, " MHz), perfctl shift ");
  589. serial_putdec(SERIAL_COM1, ratio_shift);
  590. serial_puts(SERIAL_COM1, "\npower: eist ");
  591. serial_puts(SERIAL_COM1, eist_on ? "on" : "off");
  592. serial_puts(SERIAL_COM1, ", turbo ");
  593. serial_puts(SERIAL_COM1, turbo_disabled ? "disabled" : "enabled");
  594. serial_puts(SERIAL_COM1, ", c1e ");
  595. serial_puts(SERIAL_COM1, c1e_on ? "on" : "off");
  596. serial_puts(SERIAL_COM1, ", cst ");
  597. serial_putdec(SERIAL_COM1, cst_limit);
  598. serial_puts(SERIAL_COM1, cst_locked ? " locked" : " unlocked");
  599. serial_puts(SERIAL_COM1, ", arat ");
  600. serial_puts(SERIAL_COM1, has_arat ? "yes" : "no");
  601. serial_puts(SERIAL_COM1, ", pwr mgmt ");
  602. serial_puthex(SERIAL_COM1, lo);
  603. serial_puts(SERIAL_COM1, "\n");
  604. }
  605. int power_idle_hint(void)
  606. {
  607. if (!supported)
  608. return -1;
  609. if (platform == PLAT_AIRMONT) {
  610. /* C6N is reported by CPUID, but the hint stays disabled
  611. * until the read-only probe has booted cleanly on target. */
  612. if (!c6n_enabled || !c6n_ok)
  613. return -1;
  614. /* An AP without ARAT may lose its local APIC timer, so it
  615. * falls back to HLT (C1). */
  616. if (this_cpu()->id != 0 && !has_arat)
  617. return -1;
  618. return MWAIT_C6N;
  619. }
  620. /* An AP can only use C3/C6 when its local APIC timer keeps running
  621. * there (ARAT); otherwise C1E is the deepest safe state and the BSP
  622. * PIT still wakes it up via IPIs. */
  623. if (this_cpu()->id != 0 && !has_arat)
  624. return 0x01;
  625. return 0x20; /* C6 hint: the CPU uses the deepest state it has */
  626. }
  627. void power_tick(int idle, unsigned int ms)
  628. {
  629. struct cpu *c = this_cpu();
  630. struct power_cpu *p;
  631. unsigned int load, r;
  632. if (!supported || c->id >= MAX_CPUS || ms == 0)
  633. return;
  634. p = &pcs[c->id];
  635. /* ---- P-state governor (100 ms windows) ---- */
  636. if (pstate_writes && cpufreq_mode == POWER_CPUFREQ_AUTO) {
  637. if (idle) {
  638. p->streak_ms = 0;
  639. } else {
  640. p->busy_ms += ms;
  641. if (p->streak_ms < 1000)
  642. p->streak_ms += ms;
  643. /* A short busy streak means interactive work or a
  644. * command: jump to max immediately, then let the next
  645. * window decide. */
  646. if (p->streak_ms >= 4 && p->req != max_ratio)
  647. power_apply(p, max_ratio);
  648. }
  649. p->div += ms;
  650. if (p->div >= 100) {
  651. load = p->busy_ms * 100u / p->div;
  652. p->div = 0;
  653. p->busy_ms = 0;
  654. if (load < 15)
  655. r = min_ratio;
  656. else if (load > 80)
  657. r = max_ratio;
  658. else
  659. r = min_ratio +
  660. (max_ratio - min_ratio) * load / 100u;
  661. power_apply(p, r);
  662. }
  663. } else if (pstate_writes && cpufreq_mode != POWER_CPUFREQ_OFF) {
  664. if (cpufreq_mode == POWER_CPUFREQ_MIN)
  665. r = min_ratio;
  666. else if (cpufreq_mode == POWER_CPUFREQ_MAX)
  667. r = max_ratio;
  668. else
  669. r = fixed_ratio;
  670. power_apply(p, r);
  671. }
  672. /* ---- once-a-second clock statistics ---- */
  673. p->stat_div += ms;
  674. if (p->stat_div < 1000)
  675. return;
  676. p->stat_div = 0;
  677. if (platform == PLAT_AIRMONT) {
  678. unsigned long long aperf, mperf;
  679. unsigned int da, dm;
  680. if (!has_aperf) {
  681. /* The CLOCK_MODULATION/APERF capability is absent;
  682. * report the PERF_STATUS ratio only. */
  683. rdmsr(MSR_IA32_PERF_STATUS, &da, &dm);
  684. p->status = da & 0xFFFFu;
  685. r = (p->status >> 8) & 0xFFu;
  686. if (r >= min_ratio && r <= max_ratio)
  687. p->ratio = r;
  688. else
  689. p->ratio = p->req;
  690. return;
  691. }
  692. aperf = rdmsr64(MSR_APERF);
  693. mperf = rdmsr64(MSR_MPERF);
  694. if (!p->have_last) {
  695. p->aperf = aperf;
  696. p->mperf = mperf;
  697. p->have_last = 1;
  698. return;
  699. }
  700. da = (unsigned int)((aperf - p->aperf) >> 12);
  701. dm = (unsigned int)((mperf - p->mperf) >> 12);
  702. p->aperf = aperf;
  703. p->mperf = mperf;
  704. p->mhz = dm ? da * nominal_mhz / dm : 0;
  705. rdmsr(MSR_IA32_PERF_STATUS, &da, &dm);
  706. p->status = da & 0xFFFFu;
  707. r = (p->status >> 8) & 0xFFu;
  708. if (r >= min_ratio && r <= max_ratio)
  709. p->ratio = r;
  710. else
  711. p->ratio = p->req;
  712. return;
  713. }
  714. {
  715. unsigned long long aperf = rdmsr64(MSR_APERF);
  716. unsigned long long mperf = rdmsr64(MSR_MPERF);
  717. unsigned long long tsc = rdmsr64(MSR_TSC);
  718. unsigned long long c3 = rdmsr64(MSR_CORE_C3_RESIDENCY);
  719. unsigned long long c6 = rdmsr64(MSR_CORE_C6_RESIDENCY);
  720. unsigned int dtsc, d3, d6, da, dm;
  721. if (!p->have_last) {
  722. p->aperf = aperf;
  723. p->mperf = mperf;
  724. p->c3 = c3;
  725. p->c6 = c6;
  726. p->tsc = tsc;
  727. p->have_last = 1;
  728. return;
  729. }
  730. dtsc = (unsigned int)((tsc - p->tsc) >> 12);
  731. d3 = (unsigned int)((c3 - p->c3) >> 12);
  732. d6 = (unsigned int)((c6 - p->c6) >> 12);
  733. da = (unsigned int)((aperf - p->aperf) >> 12);
  734. dm = (unsigned int)((mperf - p->mperf) >> 12);
  735. p->aperf = aperf;
  736. p->mperf = mperf;
  737. p->c3 = c3;
  738. p->c6 = c6;
  739. p->tsc = tsc;
  740. p->pc3 = dtsc ? d3 * 1000u / dtsc : 0;
  741. p->pc6 = dtsc ? d6 * 1000u / dtsc : 0;
  742. if (p->pc3 > 1000)
  743. p->pc3 = 1000;
  744. if (p->pc6 > 1000)
  745. p->pc6 = 1000;
  746. p->mhz = dm ? da * nominal_mhz / dm : 0;
  747. /* IA32_PERF_STATUS reports the current ratio in bits 7:0
  748. * (the boot dump shows 0x12 = 18 at the max non-turbo ratio),
  749. * unlike PERF_CTL, which takes the target in bits 15:8. */
  750. rdmsr(MSR_IA32_PERF_STATUS, &da, &dm);
  751. p->status = da & 0xFFFFu;
  752. p->ratio = (platform == PLAT_KABYLAKE) ?
  753. (p->status >> 8) & 0xFFu :
  754. p->status & 0xFFu;
  755. if (c->id == 0) {
  756. unsigned long long pc3 = rdmsr64(MSR_PKG_C3_RESIDENCY);
  757. unsigned long long pc6 = rdmsr64(MSR_PKG_C6_RESIDENCY);
  758. if (pkg_have_last) {
  759. pkg_pc3 = dtsc ?
  760. (unsigned int)((pc3 - pkg_c3_last) >> 12) *
  761. 1000u / dtsc : 0;
  762. pkg_pc6 = dtsc ?
  763. (unsigned int)((pc6 - pkg_c6_last) >> 12) *
  764. 1000u / dtsc : 0;
  765. if (pkg_pc3 > 1000)
  766. pkg_pc3 = 1000;
  767. if (pkg_pc6 > 1000)
  768. pkg_pc6 = 1000;
  769. }
  770. pkg_c3_last = pc3;
  771. pkg_c6_last = pc6;
  772. pkg_have_last = 1;
  773. }
  774. }
  775. }
  776. const char *power_cpufreq_name(void)
  777. {
  778. switch (cpufreq_mode) {
  779. case POWER_CPUFREQ_MIN:
  780. return "min";
  781. case POWER_CPUFREQ_MAX:
  782. return "max";
  783. case POWER_CPUFREQ_OFF:
  784. return "off";
  785. case POWER_CPUFREQ_FIXED:
  786. return "fixed";
  787. default:
  788. return "auto";
  789. }
  790. }
  791. int power_cpufreq_mode(void)
  792. {
  793. if (!supported || !pstate_writes)
  794. return -1;
  795. return cpufreq_mode;
  796. }
  797. int power_cpufreq_set_mode(int mode)
  798. {
  799. if (!supported || !pstate_writes)
  800. return -1;
  801. if (mode != POWER_CPUFREQ_AUTO && mode != POWER_CPUFREQ_MIN &&
  802. mode != POWER_CPUFREQ_MAX && mode != POWER_CPUFREQ_OFF)
  803. return -1;
  804. cpufreq_mode = mode;
  805. return cpufreq_mode;
  806. }
  807. static void put_dec(unsigned int v)
  808. {
  809. char buf[12];
  810. unsigned int n = 0;
  811. if (!v) {
  812. io_putchar('0');
  813. return;
  814. }
  815. while (v && n < sizeof(buf)) {
  816. buf[n++] = (char)('0' + v % 10);
  817. v /= 10;
  818. }
  819. while (n)
  820. io_putchar(buf[--n]);
  821. }
  822. static void put_pct(unsigned int permille)
  823. {
  824. put_dec(permille / 10);
  825. io_putchar('.');
  826. io_putchar((char)('0' + permille % 10));
  827. io_putchar('%');
  828. }
  829. void power_print(void)
  830. {
  831. unsigned int i;
  832. unsigned int lo, hi;
  833. if (!supported) {
  834. io_putstr("power: not available on this CPU\n");
  835. return;
  836. }
  837. io_putstr("power: ");
  838. io_putstr(platform == PLAT_AIRMONT ? "airmont/braswell" :
  839. platform == PLAT_KABYLAKE ? "kaby lake/hwp" :
  840. "nehalem/westmere");
  841. if (!pstate_writes)
  842. io_putstr(" (probe only)");
  843. io_putstr(", idle ");
  844. io_putstr(idle_mode_name(idle_mode()));
  845. if (platform == PLAT_AIRMONT)
  846. io_putstr((c6n_enabled && idle_mwait_supported()) ?
  847. " (mwait c6n)" : " (hlt)");
  848. io_putstr(", cpufreq ");
  849. if (!pstate_writes)
  850. io_putstr("probe");
  851. else {
  852. io_putstr(power_cpufreq_name());
  853. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  854. io_putchar(' ');
  855. put_dec(fixed_ratio);
  856. }
  857. }
  858. io_putstr(" (pstates ");
  859. put_dec(min_ratio);
  860. io_putstr("..");
  861. put_dec(max_ratio);
  862. io_putstr(")\n");
  863. if (platform == PLAT_AIRMONT) {
  864. io_putstr("eist ");
  865. io_putstr(eist_on ? "on" : "off");
  866. io_putstr(", turbo ");
  867. io_putstr(turbo_disabled ? "disabled" : "enabled");
  868. io_putstr(", arat ");
  869. io_putstr(has_arat ? "yes" : "no");
  870. io_putstr(", c6n ");
  871. if (!c6n_ok)
  872. io_putstr("no");
  873. else if (c6n_enabled)
  874. io_putstr("yes");
  875. else
  876. io_putstr("yes (disabled)");
  877. io_putstr("\nfsb ");
  878. put_dec(fsb_khz);
  879. io_putstr(" kHz, nominal ");
  880. put_dec(nominal_mhz);
  881. io_putstr(" MHz, turbo p");
  882. put_dec(turbo_ratio);
  883. io_putstr(" vid 0x");
  884. io_puthex(turbo_vid);
  885. io_putstr(", vid 0x");
  886. io_puthex(atom_vid_min);
  887. io_putstr("..0x");
  888. io_puthex(atom_vid_max);
  889. io_putstr(atom_vid_valid ? "" : " (invalid)");
  890. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  891. io_putstr(", perf ctl 0x");
  892. io_puthex(lo);
  893. io_putchar('\n');
  894. for (i = 0; i < cpu_count && i < MAX_CPUS; i++) {
  895. struct power_cpu *p = &pcs[i];
  896. io_putstr("cpu");
  897. put_dec(i);
  898. io_putchar(' ');
  899. put_dec(p->mhz);
  900. io_putstr(" MHz cur r");
  901. if (p->ratio >= min_ratio && p->ratio <= max_ratio) {
  902. if (p->ratio < 10)
  903. io_putchar('0');
  904. put_dec(p->ratio);
  905. } else {
  906. io_putstr("--");
  907. }
  908. io_putstr(" req r");
  909. if (p->req < 10)
  910. io_putchar('0');
  911. put_dec(p->req);
  912. io_putchar('\n');
  913. }
  914. return;
  915. }
  916. if (platform == PLAT_KABYLAKE) {
  917. io_putstr("hwp ");
  918. io_putstr(hwp ? "on" : "off");
  919. io_putstr(", epp ");
  920. put_dec(hwp_epp);
  921. io_putstr(", turbo p");
  922. put_dec(turbo_ratio);
  923. io_putstr(", eist ");
  924. io_putstr(eist_on ? "on" : "off");
  925. io_putstr(", arat ");
  926. io_putstr(has_arat ? "yes\n" : "no\n");
  927. }
  928. io_putstr("cst ");
  929. put_dec(cst_limit);
  930. io_putstr(cst_locked ? " locked" : " unlocked");
  931. io_putstr(", c1e ");
  932. io_putstr(c1e_on ? "on" : "off");
  933. io_putstr(", arat ");
  934. io_putstr(has_arat ? "yes\n" : "no\n");
  935. io_putstr("pkg c3 ");
  936. put_pct(pkg_pc3);
  937. io_putstr(" c6 ");
  938. put_pct(pkg_pc6);
  939. io_putchar('\n');
  940. for (i = 0; i < cpu_count && i < MAX_CPUS; i++) {
  941. struct power_cpu *p = &pcs[i];
  942. io_putstr("cpu");
  943. put_dec(i);
  944. io_putchar(' ');
  945. put_dec(p->mhz);
  946. io_putstr(" MHz cur r");
  947. if (p->ratio >= min_ratio && p->ratio <= max_ratio + 8) {
  948. if (p->ratio < 10)
  949. io_putchar('0');
  950. put_dec(p->ratio);
  951. } else {
  952. io_putstr("--");
  953. }
  954. io_putstr(" req r");
  955. if (p->req < 10)
  956. io_putchar('0');
  957. put_dec(p->req);
  958. io_putstr(" c3 ");
  959. put_pct(p->pc3);
  960. io_putstr(" c6 ");
  961. put_pct(p->pc6);
  962. io_putchar('\n');
  963. }
  964. }
  965. int power_cpufreq_command(const char *arg)
  966. {
  967. unsigned int ratio = 0;
  968. int have_ratio = 0;
  969. while (*arg == ' ')
  970. arg++;
  971. if (!supported) {
  972. io_putstr("cpufreq: not available on this CPU\n");
  973. return -1;
  974. }
  975. if (!pstate_writes) {
  976. if (!*arg) {
  977. io_putstr("cpufreq probe (P-state writes disabled)\n");
  978. return 0;
  979. }
  980. io_putstr("cpufreq: P-state writes are disabled in this probe build\n");
  981. return -1;
  982. }
  983. if (!*arg) {
  984. io_putstr("cpufreq ");
  985. io_putstr(power_cpufreq_name());
  986. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  987. io_putchar(' ');
  988. put_dec(fixed_ratio);
  989. }
  990. io_putchar('\n');
  991. return 0;
  992. }
  993. if (arg[0] == 'a' && arg[1] == 'u' && arg[2] == 't' &&
  994. arg[3] == 'o' && !arg[4]) {
  995. cpufreq_mode = POWER_CPUFREQ_AUTO;
  996. } else if (arg[0] == 'm' && arg[1] == 'i' && arg[2] == 'n' &&
  997. !arg[3]) {
  998. cpufreq_mode = POWER_CPUFREQ_MIN;
  999. } else if (arg[0] == 'm' && arg[1] == 'a' && arg[2] == 'x' &&
  1000. !arg[3]) {
  1001. cpufreq_mode = POWER_CPUFREQ_MAX;
  1002. } else if (arg[0] == 'o' && arg[1] == 'f' && arg[2] == 'f' &&
  1003. !arg[3]) {
  1004. cpufreq_mode = POWER_CPUFREQ_OFF;
  1005. } else {
  1006. while (*arg >= '0' && *arg <= '9') {
  1007. ratio = ratio * 10u + (unsigned int)(*arg - '0');
  1008. arg++;
  1009. have_ratio = 1;
  1010. }
  1011. if (!have_ratio || *arg || ratio < min_ratio ||
  1012. ratio > max_ratio) {
  1013. io_putstr("cpufreq: usage cpufreq [auto|min|max|off|<ratio>]\n");
  1014. return -1;
  1015. }
  1016. fixed_ratio = ratio;
  1017. cpufreq_mode = POWER_CPUFREQ_FIXED;
  1018. }
  1019. io_putstr("cpufreq ");
  1020. io_putstr(power_cpufreq_name());
  1021. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  1022. io_putchar(' ');
  1023. put_dec(fixed_ratio);
  1024. }
  1025. io_putchar('\n');
  1026. return 0;
  1027. }