power.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* Intel Nehalem/Westmere power management: P-states and deep C-states.
  2. *
  3. * The kernel has no ACPI interpreter and no cpufreq driver, so the CPU used
  4. * to stay at whatever P-state the BIOS left (the maximum non-turbo ratio)
  5. * and only ever reached C1 (HLT). An idle X201 therefore ran at 2.4 GHz
  6. * with all four threads in C1, which kept the package near 65 C and the EC
  7. * fan around 3700 RPM.
  8. *
  9. * This module adds the two things the hardware needs from the OS:
  10. * - a small race-to-idle P-state governor writing IA32_PERF_CTL (lowest
  11. * ratio for idle CPUs, maximum non-turbo ratio for a CPU that is running
  12. * something, linear in between for mixed windows), and
  13. * - deep C-state hints for idle.c (C1E/C3/C6 through MONITOR/MWAIT).
  14. *
  15. * Everything is gated on the Nehalem/Westmere model list: those are the
  16. * parts known to implement the MSRs used here, and the Atom boards keep
  17. * their old HLT behaviour. The module only touches documented MSRs.
  18. */
  19. #include "power.h"
  20. #include "cpu.h"
  21. #include "idle.h"
  22. #include "io.h"
  23. #include "sched.h"
  24. #include "serial.h"
  25. #define MSR_IA32_PERF_STATUS 0x198
  26. #define MSR_IA32_PERF_CTL 0x199
  27. #define MSR_PLATFORM_INFO 0x0CE
  28. #define MSR_MISC_ENABLE 0x1A0
  29. #define MSR_MISC_PWR_MGMT 0x1AA
  30. #define MSR_POWER_CTL 0x1FC
  31. #define MSR_PKG_CST_CONFIG 0x0E2
  32. #define MSR_PKG_C3_RESIDENCY 0x3F8
  33. #define MSR_PKG_C6_RESIDENCY 0x3F9
  34. #define MSR_CORE_C3_RESIDENCY 0x3FC
  35. #define MSR_CORE_C6_RESIDENCY 0x3FD
  36. #define MSR_APERF 0x0E8
  37. #define MSR_MPERF 0x0E7
  38. #define MSR_TSC 0x010
  39. /* P-state ratios are the bus-ratio multipliers (133.33 MHz on Arrandale). */
  40. #define BCLK_MHZ 133u
  41. struct power_cpu {
  42. /* Governor state (100 ms windows). */
  43. unsigned int div; /* ms in this window */
  44. unsigned int busy_ms; /* ms this CPU ran a non-idle proc */
  45. unsigned int streak_ms; /* consecutive busy ms */
  46. unsigned int req; /* last ratio written */
  47. /* Once-a-second statistics. */
  48. unsigned int stat_div;
  49. int have_last;
  50. unsigned long long aperf, mperf, c3, c6, tsc;
  51. unsigned int mhz;
  52. unsigned int ratio;
  53. unsigned int status; /* raw IA32_PERF_STATUS[15:0] */
  54. unsigned int pc3, pc6; /* permille */
  55. };
  56. static struct power_cpu pcs[MAX_CPUS];
  57. static int supported;
  58. static int has_arat;
  59. static int eist_on;
  60. static int turbo_disabled;
  61. static int c1e_on;
  62. static unsigned int cst_limit;
  63. static int cst_locked;
  64. static unsigned int min_ratio = 6;
  65. static unsigned int max_ratio;
  66. static unsigned int ratio_shift;
  67. static unsigned int nominal_mhz;
  68. static int cpufreq_mode = POWER_CPUFREQ_AUTO;
  69. static unsigned int fixed_ratio;
  70. static unsigned long long pkg_c3_last, pkg_c6_last;
  71. static unsigned int pkg_pc3, pkg_pc6;
  72. static int pkg_have_last;
  73. static void cpuid_leaf(unsigned int leaf, unsigned int *a, unsigned int *b,
  74. unsigned int *c, unsigned int *d)
  75. {
  76. __asm__ volatile ("cpuid"
  77. : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
  78. : "a"(leaf), "c"(0));
  79. }
  80. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  81. {
  82. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  83. }
  84. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  85. {
  86. __asm__ volatile ("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));
  87. }
  88. static unsigned long long rdmsr64(unsigned int msr)
  89. {
  90. unsigned int lo, hi;
  91. rdmsr(msr, &lo, &hi);
  92. return ((unsigned long long)hi << 32) | lo;
  93. }
  94. /* Model allowlist: the Nehalem/Westmere family that implements the MSRs
  95. * above. Bonnell (0x1C/0x1D) is deliberately absent. */
  96. static int power_model(unsigned int model)
  97. {
  98. switch (model) {
  99. case 0x1A: /* Nehalem */
  100. case 0x1E: /* Lynnfield/Clarksfield */
  101. case 0x1F: /* Nehalem-EX */
  102. case 0x25: /* Arrandale/Clarkdale (X201 i5 M 520) */
  103. case 0x2C: /* Westmere-EP */
  104. case 0x2E: /* Nehalem-EX */
  105. case 0x2F: /* Westmere-EX */
  106. return 1;
  107. default:
  108. return 0;
  109. }
  110. }
  111. /* Write one target ratio. Nehalem/Westmere use bits 15:8 of
  112. * IA32_PERF_CTL for the target ratio; the low byte is reserved and written
  113. * as zero (coreboot does the same). */
  114. static void power_write_ratio(unsigned int ratio)
  115. {
  116. unsigned int lo, hi;
  117. if (ratio < min_ratio)
  118. ratio = min_ratio;
  119. if (ratio > max_ratio)
  120. ratio = max_ratio;
  121. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  122. if (ratio_shift) {
  123. /* Bits 15:8 carry the ratio and 7:0 are reserved (the old VID
  124. * slot); coreboot writes them as zero. Keeping the BIOS VID
  125. * byte here made the request silently ineffective. */
  126. lo = (lo & 0xFFFF0000u) | (ratio << 8);
  127. } else {
  128. lo = (lo & ~0xFFu) | ratio;
  129. }
  130. wrmsr(MSR_IA32_PERF_CTL, lo, hi);
  131. }
  132. static void power_apply(struct power_cpu *p, unsigned int ratio)
  133. {
  134. if (ratio < min_ratio)
  135. ratio = min_ratio;
  136. if (ratio > max_ratio)
  137. ratio = max_ratio;
  138. if (ratio == p->req)
  139. return;
  140. power_write_ratio(ratio);
  141. p->req = ratio;
  142. }
  143. void power_init(void)
  144. {
  145. unsigned int max_leaf, a, b, c, d;
  146. unsigned int family, model;
  147. unsigned int lo, hi;
  148. supported = 0;
  149. has_arat = 0;
  150. cpufreq_mode = POWER_CPUFREQ_AUTO;
  151. if (!cpu_has_cpuid()) {
  152. serial_puts(SERIAL_COM1, "power: no CPUID, disabled\n");
  153. return;
  154. }
  155. cpuid_leaf(0, &max_leaf, &b, &c, &d);
  156. if (max_leaf < 1) {
  157. serial_puts(SERIAL_COM1, "power: no CPUID leaf 1\n");
  158. return;
  159. }
  160. cpuid_leaf(1, &a, &b, &c, &d);
  161. family = (a >> 8) & 0xF;
  162. model = (a >> 4) & 0xF;
  163. if (family == 0xF)
  164. family += (a >> 20) & 0xFF;
  165. if (family == 0x6 || family == 0xF)
  166. model += ((a >> 16) & 0xF) << 4;
  167. if (family != 0x6 || !power_model(model)) {
  168. serial_puts(SERIAL_COM1, "power: CPU family ");
  169. serial_putdec(SERIAL_COM1, family);
  170. serial_puts(SERIAL_COM1, " model ");
  171. serial_putdec(SERIAL_COM1, model);
  172. serial_puts(SERIAL_COM1, ", no Nehalem/Westmere PM\n");
  173. return;
  174. }
  175. rdmsr(MSR_PLATFORM_INFO, &lo, &hi);
  176. max_ratio = (lo >> 8) & 0xFF;
  177. min_ratio = (hi >> 8) & 0xFF;
  178. if (max_ratio < 5 || max_ratio > 60 || min_ratio < 3 ||
  179. min_ratio > max_ratio) {
  180. serial_puts(SERIAL_COM1, "power: implausible platform info, disabled\n");
  181. return;
  182. }
  183. nominal_mhz = max_ratio * BCLK_MHZ + max_ratio / 3;
  184. /* Nehalem/Westmere keep the target ratio in bits 15:8 of
  185. * IA32_PERF_CTL/STATUS; the low byte is the (now unused) VID field.
  186. * coreboot's set_max_ratio writes (ratio << 8) for this family, and
  187. * reading the low byte on the X201 gives VID-looking values (0x11,
  188. * 0x18) that would otherwise masquerade as ratios. */
  189. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  190. ratio_shift = 8;
  191. if (max_leaf >= 6) {
  192. cpuid_leaf(6, &a, &b, &c, &d);
  193. has_arat = (a & 4u) ? 1 : 0;
  194. }
  195. rdmsr(MSR_MISC_ENABLE, &lo, &hi);
  196. eist_on = (lo >> 16) & 1;
  197. turbo_disabled = (hi >> 6) & 1;
  198. if (!eist_on) {
  199. wrmsr(MSR_MISC_ENABLE, lo | (1u << 16), hi);
  200. eist_on = 1;
  201. }
  202. rdmsr(MSR_POWER_CTL, &lo, &hi);
  203. c1e_on = (lo >> 1) & 1;
  204. if (!c1e_on) {
  205. wrmsr(MSR_POWER_CTL, lo | (1u << 1), hi);
  206. c1e_on = 1;
  207. }
  208. rdmsr(MSR_PKG_CST_CONFIG, &lo, &hi);
  209. cst_limit = lo & 7u;
  210. cst_locked = (lo >> 15) & 1;
  211. if (!cst_locked && cst_limit < 3) {
  212. wrmsr(MSR_PKG_CST_CONFIG, (lo & ~7u) | 3u, hi);
  213. cst_limit = 3;
  214. }
  215. rdmsr(MSR_MISC_PWR_MGMT, &lo, &hi);
  216. supported = 1;
  217. /* Start at the maximum non-turbo ratio; the governor drops to the
  218. * minimum after the first idle window. */
  219. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  220. serial_puts(SERIAL_COM1, "power: perf status ");
  221. serial_puthex(SERIAL_COM1, lo);
  222. pcs[0].req = max_ratio;
  223. power_write_ratio(max_ratio);
  224. rdmsr(MSR_IA32_PERF_CTL, &lo, &hi);
  225. serial_puts(SERIAL_COM1, ", perf ctl ");
  226. serial_puthex(SERIAL_COM1, lo);
  227. rdmsr(MSR_IA32_PERF_STATUS, &lo, &hi);
  228. serial_puts(SERIAL_COM1, ", status now ");
  229. serial_puthex(SERIAL_COM1, lo);
  230. serial_puts(SERIAL_COM1, "\n");
  231. pcs[0].aperf = rdmsr64(MSR_APERF);
  232. pcs[0].mperf = rdmsr64(MSR_MPERF);
  233. pcs[0].c3 = rdmsr64(MSR_CORE_C3_RESIDENCY);
  234. pcs[0].c6 = rdmsr64(MSR_CORE_C6_RESIDENCY);
  235. pcs[0].tsc = rdmsr64(MSR_TSC);
  236. pcs[0].have_last = 1;
  237. pkg_c3_last = rdmsr64(MSR_PKG_C3_RESIDENCY);
  238. pkg_c6_last = rdmsr64(MSR_PKG_C6_RESIDENCY);
  239. pkg_have_last = 1;
  240. serial_puts(SERIAL_COM1, "power: Nehalem/Westmere, ratios ");
  241. serial_putdec(SERIAL_COM1, min_ratio);
  242. serial_puts(SERIAL_COM1, "..");
  243. serial_putdec(SERIAL_COM1, max_ratio);
  244. serial_puts(SERIAL_COM1, " (");
  245. serial_putdec(SERIAL_COM1, nominal_mhz);
  246. serial_puts(SERIAL_COM1, " MHz), perfctl shift ");
  247. serial_putdec(SERIAL_COM1, ratio_shift);
  248. serial_puts(SERIAL_COM1, "\npower: eist ");
  249. serial_puts(SERIAL_COM1, eist_on ? "on" : "off");
  250. serial_puts(SERIAL_COM1, ", turbo ");
  251. serial_puts(SERIAL_COM1, turbo_disabled ? "disabled" : "enabled");
  252. serial_puts(SERIAL_COM1, ", c1e ");
  253. serial_puts(SERIAL_COM1, c1e_on ? "on" : "off");
  254. serial_puts(SERIAL_COM1, ", cst ");
  255. serial_putdec(SERIAL_COM1, cst_limit);
  256. serial_puts(SERIAL_COM1, cst_locked ? " locked" : " unlocked");
  257. serial_puts(SERIAL_COM1, ", arat ");
  258. serial_puts(SERIAL_COM1, has_arat ? "yes" : "no");
  259. serial_puts(SERIAL_COM1, ", pwr mgmt ");
  260. serial_puthex(SERIAL_COM1, lo);
  261. serial_puts(SERIAL_COM1, "\n");
  262. }
  263. int power_idle_hint(void)
  264. {
  265. if (!supported)
  266. return -1;
  267. /* An AP can only use C3/C6 when its local APIC timer keeps running
  268. * there (ARAT); otherwise C1E is the deepest safe state and the BSP
  269. * PIT still wakes it up via IPIs. */
  270. if (this_cpu()->id != 0 && !has_arat)
  271. return 0x01;
  272. return 0x20; /* C6 hint: the CPU uses the deepest state it has */
  273. }
  274. void power_tick(int idle, unsigned int ms)
  275. {
  276. struct cpu *c = this_cpu();
  277. struct power_cpu *p;
  278. unsigned int load, r;
  279. if (!supported || c->id >= MAX_CPUS || ms == 0)
  280. return;
  281. p = &pcs[c->id];
  282. /* ---- P-state governor (100 ms windows) ---- */
  283. if (cpufreq_mode == POWER_CPUFREQ_AUTO) {
  284. if (idle) {
  285. p->streak_ms = 0;
  286. } else {
  287. p->busy_ms += ms;
  288. if (p->streak_ms < 1000)
  289. p->streak_ms += ms;
  290. /* A short busy streak means interactive work or a
  291. * command: jump to max immediately, then let the next
  292. * window decide. */
  293. if (p->streak_ms >= 4 && p->req != max_ratio)
  294. power_apply(p, max_ratio);
  295. }
  296. p->div += ms;
  297. if (p->div >= 100) {
  298. load = p->busy_ms * 100u / p->div;
  299. p->div = 0;
  300. p->busy_ms = 0;
  301. if (load < 15)
  302. r = min_ratio;
  303. else if (load > 80)
  304. r = max_ratio;
  305. else
  306. r = min_ratio +
  307. (max_ratio - min_ratio) * load / 100u;
  308. power_apply(p, r);
  309. }
  310. } else if (cpufreq_mode != POWER_CPUFREQ_OFF) {
  311. if (cpufreq_mode == POWER_CPUFREQ_MIN)
  312. r = min_ratio;
  313. else if (cpufreq_mode == POWER_CPUFREQ_MAX)
  314. r = max_ratio;
  315. else
  316. r = fixed_ratio;
  317. power_apply(p, r);
  318. }
  319. /* ---- once-a-second residency/clock statistics ---- */
  320. p->stat_div += ms;
  321. if (p->stat_div < 1000)
  322. return;
  323. p->stat_div = 0;
  324. {
  325. unsigned long long aperf = rdmsr64(MSR_APERF);
  326. unsigned long long mperf = rdmsr64(MSR_MPERF);
  327. unsigned long long tsc = rdmsr64(MSR_TSC);
  328. unsigned long long c3 = rdmsr64(MSR_CORE_C3_RESIDENCY);
  329. unsigned long long c6 = rdmsr64(MSR_CORE_C6_RESIDENCY);
  330. unsigned int dtsc, d3, d6, da, dm;
  331. if (!p->have_last) {
  332. p->aperf = aperf;
  333. p->mperf = mperf;
  334. p->c3 = c3;
  335. p->c6 = c6;
  336. p->tsc = tsc;
  337. p->have_last = 1;
  338. return;
  339. }
  340. dtsc = (unsigned int)((tsc - p->tsc) >> 12);
  341. d3 = (unsigned int)((c3 - p->c3) >> 12);
  342. d6 = (unsigned int)((c6 - p->c6) >> 12);
  343. da = (unsigned int)((aperf - p->aperf) >> 12);
  344. dm = (unsigned int)((mperf - p->mperf) >> 12);
  345. p->aperf = aperf;
  346. p->mperf = mperf;
  347. p->c3 = c3;
  348. p->c6 = c6;
  349. p->tsc = tsc;
  350. p->pc3 = dtsc ? d3 * 1000u / dtsc : 0;
  351. p->pc6 = dtsc ? d6 * 1000u / dtsc : 0;
  352. if (p->pc3 > 1000)
  353. p->pc3 = 1000;
  354. if (p->pc6 > 1000)
  355. p->pc6 = 1000;
  356. p->mhz = dm ? da * nominal_mhz / dm : 0;
  357. /* IA32_PERF_STATUS reports the current ratio in bits 7:0
  358. * (the boot dump shows 0x12 = 18 at the max non-turbo ratio),
  359. * unlike PERF_CTL, which takes the target in bits 15:8. */
  360. rdmsr(MSR_IA32_PERF_STATUS, &da, &dm);
  361. p->status = da & 0xFFFFu;
  362. p->ratio = p->status & 0xFFu;
  363. if (c->id == 0) {
  364. unsigned long long pc3 = rdmsr64(MSR_PKG_C3_RESIDENCY);
  365. unsigned long long pc6 = rdmsr64(MSR_PKG_C6_RESIDENCY);
  366. if (pkg_have_last) {
  367. pkg_pc3 = dtsc ?
  368. (unsigned int)((pc3 - pkg_c3_last) >> 12) *
  369. 1000u / dtsc : 0;
  370. pkg_pc6 = dtsc ?
  371. (unsigned int)((pc6 - pkg_c6_last) >> 12) *
  372. 1000u / dtsc : 0;
  373. if (pkg_pc3 > 1000)
  374. pkg_pc3 = 1000;
  375. if (pkg_pc6 > 1000)
  376. pkg_pc6 = 1000;
  377. }
  378. pkg_c3_last = pc3;
  379. pkg_c6_last = pc6;
  380. pkg_have_last = 1;
  381. }
  382. }
  383. }
  384. const char *power_cpufreq_name(void)
  385. {
  386. switch (cpufreq_mode) {
  387. case POWER_CPUFREQ_MIN:
  388. return "min";
  389. case POWER_CPUFREQ_MAX:
  390. return "max";
  391. case POWER_CPUFREQ_OFF:
  392. return "off";
  393. case POWER_CPUFREQ_FIXED:
  394. return "fixed";
  395. default:
  396. return "auto";
  397. }
  398. }
  399. static void put_dec(unsigned int v)
  400. {
  401. char buf[12];
  402. unsigned int n = 0;
  403. if (!v) {
  404. io_putchar('0');
  405. return;
  406. }
  407. while (v && n < sizeof(buf)) {
  408. buf[n++] = (char)('0' + v % 10);
  409. v /= 10;
  410. }
  411. while (n)
  412. io_putchar(buf[--n]);
  413. }
  414. static void put_pct(unsigned int permille)
  415. {
  416. put_dec(permille / 10);
  417. io_putchar('.');
  418. io_putchar((char)('0' + permille % 10));
  419. io_putchar('%');
  420. }
  421. void power_print(void)
  422. {
  423. unsigned int i;
  424. if (!supported) {
  425. io_putstr("power: not available on this CPU\n");
  426. return;
  427. }
  428. io_putstr("power: nehalem/westmere, idle ");
  429. io_putstr(idle_mode_name(idle_mode()));
  430. io_putstr(", cpufreq ");
  431. io_putstr(power_cpufreq_name());
  432. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  433. io_putchar(' ');
  434. put_dec(fixed_ratio);
  435. }
  436. io_putstr(" (ratios ");
  437. put_dec(min_ratio);
  438. io_putstr("..");
  439. put_dec(max_ratio);
  440. io_putstr(")\n");
  441. io_putstr("cst ");
  442. put_dec(cst_limit);
  443. io_putstr(cst_locked ? " locked" : " unlocked");
  444. io_putstr(", c1e ");
  445. io_putstr(c1e_on ? "on" : "off");
  446. io_putstr(", arat ");
  447. io_putstr(has_arat ? "yes\n" : "no\n");
  448. io_putstr("pkg c3 ");
  449. put_pct(pkg_pc3);
  450. io_putstr(" c6 ");
  451. put_pct(pkg_pc6);
  452. io_putchar('\n');
  453. for (i = 0; i < cpu_count && i < MAX_CPUS; i++) {
  454. struct power_cpu *p = &pcs[i];
  455. io_putstr("cpu");
  456. put_dec(i);
  457. io_putchar(' ');
  458. put_dec(p->mhz);
  459. io_putstr(" MHz cur r");
  460. if (p->ratio >= min_ratio && p->ratio <= max_ratio + 8) {
  461. if (p->ratio < 10)
  462. io_putchar('0');
  463. put_dec(p->ratio);
  464. } else {
  465. io_putstr("--");
  466. }
  467. io_putstr(" req r");
  468. if (p->req < 10)
  469. io_putchar('0');
  470. put_dec(p->req);
  471. io_putstr(" c3 ");
  472. put_pct(p->pc3);
  473. io_putstr(" c6 ");
  474. put_pct(p->pc6);
  475. io_putchar('\n');
  476. }
  477. }
  478. int power_cpufreq_command(const char *arg)
  479. {
  480. unsigned int ratio = 0;
  481. int have_ratio = 0;
  482. while (*arg == ' ')
  483. arg++;
  484. if (!supported) {
  485. io_putstr("cpufreq: not available on this CPU\n");
  486. return -1;
  487. }
  488. if (!*arg) {
  489. io_putstr("cpufreq ");
  490. io_putstr(power_cpufreq_name());
  491. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  492. io_putchar(' ');
  493. put_dec(fixed_ratio);
  494. }
  495. io_putchar('\n');
  496. return 0;
  497. }
  498. if (arg[0] == 'a' && arg[1] == 'u' && arg[2] == 't' &&
  499. arg[3] == 'o' && !arg[4]) {
  500. cpufreq_mode = POWER_CPUFREQ_AUTO;
  501. } else if (arg[0] == 'm' && arg[1] == 'i' && arg[2] == 'n' &&
  502. !arg[3]) {
  503. cpufreq_mode = POWER_CPUFREQ_MIN;
  504. } else if (arg[0] == 'm' && arg[1] == 'a' && arg[2] == 'x' &&
  505. !arg[3]) {
  506. cpufreq_mode = POWER_CPUFREQ_MAX;
  507. } else if (arg[0] == 'o' && arg[1] == 'f' && arg[2] == 'f' &&
  508. !arg[3]) {
  509. cpufreq_mode = POWER_CPUFREQ_OFF;
  510. } else {
  511. while (*arg >= '0' && *arg <= '9') {
  512. ratio = ratio * 10u + (unsigned int)(*arg - '0');
  513. arg++;
  514. have_ratio = 1;
  515. }
  516. if (!have_ratio || *arg || ratio < min_ratio ||
  517. ratio > max_ratio) {
  518. io_putstr("cpufreq: usage cpufreq [auto|min|max|off|<ratio>]\n");
  519. return -1;
  520. }
  521. fixed_ratio = ratio;
  522. cpufreq_mode = POWER_CPUFREQ_FIXED;
  523. }
  524. io_putstr("cpufreq ");
  525. io_putstr(power_cpufreq_name());
  526. if (cpufreq_mode == POWER_CPUFREQ_FIXED) {
  527. io_putchar(' ');
  528. put_dec(fixed_ratio);
  529. }
  530. io_putchar('\n');
  531. return 0;
  532. }