cpuinfo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* cpuinfo command: print CPU vendor/model and estimated current clock speed. */
  2. #include "idle.h"
  3. #include "io.h"
  4. #include "timer.h"
  5. #define MSR_IA32_PERF_STATUS 0x198u
  6. #define MSR_IA32_PERF_CTL 0x199u
  7. #define MSR_IA32_PLATFORM_INFO 0xCEu
  8. #define MSR_IA32_PM_ENABLE 0x770u
  9. #define MSR_IA32_HWP_CAPABILITIES 0x771u
  10. #define MSR_IA32_HWP_REQUEST 0x774u
  11. #define MSR_IA32_ENERGY_PERF_BIAS 0x1B0u
  12. #define MSR_AMD_PSTATE_CTL 0xC0010062u
  13. #define MSR_AMD_PSTATE_STATUS 0xC0010063u
  14. static void put_uint(unsigned int n)
  15. {
  16. char buf[12];
  17. char *p = buf + sizeof(buf) - 1;
  18. *p = '\0';
  19. if (n == 0) {
  20. io_putchar('0');
  21. return;
  22. }
  23. while (n) {
  24. *--p = (char)('0' + (n % 10));
  25. n /= 10;
  26. }
  27. while (*p)
  28. io_putchar(*p++);
  29. }
  30. static int streq(const char *a, const char *b)
  31. {
  32. unsigned int i = 0;
  33. while (a[i] && b[i]) {
  34. if (a[i] != b[i])
  35. return 0;
  36. i++;
  37. }
  38. return a[i] == '\0' && b[i] == '\0';
  39. }
  40. static int str_starts_with(const char *s, const char *prefix)
  41. {
  42. unsigned int i = 0;
  43. while (prefix[i]) {
  44. if (s[i] != prefix[i])
  45. return 0;
  46. i++;
  47. }
  48. return 1;
  49. }
  50. static void put_hex8(unsigned int x)
  51. {
  52. static const char hex[] = "0123456789ABCDEF";
  53. io_putchar(hex[(x >> 4) & 0xF]);
  54. io_putchar(hex[x & 0xF]);
  55. }
  56. static void put_hex32(unsigned int x)
  57. {
  58. put_hex8((x >> 24) & 0xFF);
  59. put_hex8((x >> 16) & 0xFF);
  60. put_hex8((x >> 8) & 0xFF);
  61. put_hex8(x & 0xFF);
  62. }
  63. static void cpuid(unsigned int leaf, unsigned int subleaf,
  64. unsigned int *eax, unsigned int *ebx,
  65. unsigned int *ecx, unsigned int *edx)
  66. {
  67. __asm__ volatile ("cpuid"
  68. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  69. : "a"(leaf), "c"(subleaf));
  70. }
  71. static unsigned int read_tsc_low(void)
  72. {
  73. unsigned int lo, hi;
  74. __asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
  75. (void)hi;
  76. return lo;
  77. }
  78. static void rdmsr_u32(unsigned int msr, unsigned int *lo, unsigned int *hi)
  79. {
  80. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  81. }
  82. static void wrmsr_u32(unsigned int msr, unsigned int lo, unsigned int hi)
  83. {
  84. __asm__ volatile ("wrmsr" :: "c"(msr), "a"(lo), "d"(hi));
  85. }
  86. static void trim_right_spaces(char *s)
  87. {
  88. unsigned int len = 0;
  89. while (s[len])
  90. len++;
  91. while (len > 0 && s[len - 1] == ' ') {
  92. s[len - 1] = '\0';
  93. len--;
  94. }
  95. }
  96. static void print_flag(const char *name, int *printed_any)
  97. {
  98. if (*printed_any)
  99. io_putchar(' ');
  100. io_putstr(name);
  101. *printed_any = 1;
  102. }
  103. static void print_features_leaf1(unsigned int ecx1, unsigned int edx1)
  104. {
  105. int any = 0;
  106. io_putstr("ext l1 ");
  107. if (edx1 & (1u << 0)) print_flag("FPU", &any);
  108. if (edx1 & (1u << 4)) print_flag("TSC", &any);
  109. if (edx1 & (1u << 5)) print_flag("MSR", &any);
  110. if (edx1 & (1u << 6)) print_flag("PAE", &any);
  111. if (edx1 & (1u << 9)) print_flag("APIC", &any);
  112. if (edx1 & (1u << 12)) print_flag("MTRR", &any);
  113. if (edx1 & (1u << 15)) print_flag("CMOV", &any);
  114. if (edx1 & (1u << 16)) print_flag("PAT", &any);
  115. if (edx1 & (1u << 17)) print_flag("PSE36", &any);
  116. if (edx1 & (1u << 23)) print_flag("MMX", &any);
  117. if (edx1 & (1u << 24)) print_flag("FXSR", &any);
  118. if (edx1 & (1u << 25)) print_flag("SSE", &any);
  119. if (edx1 & (1u << 26)) print_flag("SSE2", &any);
  120. if (edx1 & (1u << 28)) print_flag("HTT", &any);
  121. if (ecx1 & (1u << 0)) print_flag("SSE3", &any);
  122. if (ecx1 & (1u << 1)) print_flag("PCLMULQDQ", &any);
  123. if (ecx1 & (1u << 9)) print_flag("SSSE3", &any);
  124. if (ecx1 & (1u << 12)) print_flag("FMA", &any);
  125. if (ecx1 & (1u << 13)) print_flag("CX16", &any);
  126. if (ecx1 & (1u << 19)) print_flag("SSE4.1", &any);
  127. if (ecx1 & (1u << 20)) print_flag("SSE4.2", &any);
  128. if (ecx1 & (1u << 22)) print_flag("MOVBE", &any);
  129. if (ecx1 & (1u << 23)) print_flag("POPCNT", &any);
  130. if (ecx1 & (1u << 25)) print_flag("AES", &any);
  131. if (ecx1 & (1u << 26)) print_flag("XSAVE", &any);
  132. if (ecx1 & (1u << 27)) print_flag("OSXSAVE", &any);
  133. if (ecx1 & (1u << 28)) print_flag("AVX", &any);
  134. if (ecx1 & (1u << 29)) print_flag("F16C", &any);
  135. if (ecx1 & (1u << 30)) print_flag("RDRAND", &any);
  136. if (!any)
  137. io_putstr("none");
  138. io_putchar('\n');
  139. }
  140. static void print_features_leaf7(unsigned int max_leaf)
  141. {
  142. unsigned int eax7, ebx7, ecx7, edx7;
  143. int any = 0;
  144. if (max_leaf < 7)
  145. return;
  146. cpuid(7, 0, &eax7, &ebx7, &ecx7, &edx7);
  147. (void)eax7;
  148. (void)ecx7;
  149. (void)edx7;
  150. io_putstr("ext l7 ");
  151. if (ebx7 & (1u << 0)) print_flag("FSGSBASE", &any);
  152. if (ebx7 & (1u << 3)) print_flag("BMI1", &any);
  153. if (ebx7 & (1u << 5)) print_flag("AVX2", &any);
  154. if (ebx7 & (1u << 8)) print_flag("BMI2", &any);
  155. if (ebx7 & (1u << 9)) print_flag("ERMS", &any);
  156. if (ebx7 & (1u << 18)) print_flag("RDSEED", &any);
  157. if (ebx7 & (1u << 19)) print_flag("ADX", &any);
  158. if (ebx7 & (1u << 20)) print_flag("SMAP", &any);
  159. if (ebx7 & (1u << 23)) print_flag("CLFLUSHOPT", &any);
  160. if (ebx7 & (1u << 29)) print_flag("SHA", &any);
  161. if (!any)
  162. io_putstr("none");
  163. io_putchar('\n');
  164. }
  165. static void print_features_ext(unsigned int max_ext_leaf)
  166. {
  167. unsigned int eax, ebx, ecx, edx;
  168. int any = 0;
  169. if (max_ext_leaf < 0x80000001u)
  170. return;
  171. cpuid(0x80000001u, 0, &eax, &ebx, &ecx, &edx);
  172. (void)eax;
  173. (void)ebx;
  174. io_putstr("ext x ");
  175. if (edx & (1u << 11)) print_flag("SYSCALL", &any);
  176. if (edx & (1u << 20)) print_flag("NX", &any);
  177. if (edx & (1u << 27)) print_flag("RDTSCP", &any);
  178. if (edx & (1u << 29)) print_flag("LM", &any);
  179. if (ecx & (1u << 0)) print_flag("LAHF_LM", &any);
  180. if (ecx & (1u << 2)) print_flag("SVM", &any);
  181. if (ecx & (1u << 5)) print_flag("ABM", &any);
  182. if (ecx & (1u << 6)) print_flag("SSE4A", &any);
  183. if (ecx & (1u << 8)) print_flag("PREFETCHW", &any);
  184. if (!any)
  185. io_putstr("none");
  186. io_putchar('\n');
  187. }
  188. static void print_cpuid_frequency(unsigned int max_leaf)
  189. {
  190. unsigned int eax, ebx, ecx, edx;
  191. if (max_leaf >= 0x15u) {
  192. cpuid(0x15u, 0, &eax, &ebx, &ecx, &edx);
  193. if (eax != 0 && ebx != 0) {
  194. io_putstr("cpuid15 ratio ");
  195. put_uint(ebx);
  196. io_putchar('/');
  197. put_uint(eax);
  198. if (ecx != 0) {
  199. io_putstr(" crystal ");
  200. put_uint(ecx / 1000000u);
  201. io_putstr(" MHz");
  202. }
  203. io_putchar('\n');
  204. }
  205. }
  206. if (max_leaf >= 0x16u) {
  207. cpuid(0x16u, 0, &eax, &ebx, &ecx, &edx);
  208. if (eax != 0 || ebx != 0 || ecx != 0) {
  209. io_putstr("cpuid16 base ");
  210. put_uint(eax);
  211. io_putstr(" max ");
  212. put_uint(ebx);
  213. io_putstr(" bus ");
  214. put_uint(ecx);
  215. io_putstr(" MHz\n");
  216. }
  217. }
  218. }
  219. static void parse_family_model(unsigned int eax1,
  220. unsigned int *family_out,
  221. unsigned int *model_out,
  222. unsigned int *stepping_out)
  223. {
  224. unsigned int family = (eax1 >> 8) & 0xF;
  225. unsigned int model = (eax1 >> 4) & 0xF;
  226. unsigned int stepping = eax1 & 0xF;
  227. if (family == 0xF)
  228. family += (eax1 >> 20) & 0xFF;
  229. if (family == 0x6 || family == 0xF)
  230. model += ((eax1 >> 16) & 0xF) << 4;
  231. *family_out = family;
  232. *model_out = model;
  233. *stepping_out = stepping;
  234. }
  235. void program_cpuinfo_main(void)
  236. {
  237. unsigned int eax, ebx, ecx, edx;
  238. unsigned int ecx1, edx1, ebx1;
  239. char vendor[13];
  240. char brand[49];
  241. unsigned int max_leaf;
  242. unsigned int max_ext_leaf;
  243. unsigned int family, model, stepping;
  244. unsigned int logical_cpus = 1;
  245. unsigned int cores = 1;
  246. unsigned int threads_per_core = 1;
  247. unsigned int start_ms, end_ms, delta_ms;
  248. unsigned int t0, t1, delta_cycles;
  249. unsigned int mhz_x10;
  250. unsigned int has_brand = 0;
  251. unsigned int i;
  252. cpuid(0, 0, &max_leaf, &ebx, &ecx, &edx);
  253. ((unsigned int *)vendor)[0] = ebx;
  254. ((unsigned int *)vendor)[1] = edx;
  255. ((unsigned int *)vendor)[2] = ecx;
  256. vendor[12] = '\0';
  257. cpuid(0x80000000u, 0, &max_ext_leaf, &ebx, &ecx, &edx);
  258. io_putstr("vendor ");
  259. io_putstr(vendor);
  260. io_putchar('\n');
  261. io_putstr("cpuid max ");
  262. put_uint(max_leaf);
  263. io_putstr(" ext max ");
  264. put_uint(max_ext_leaf & 0xFFFFu);
  265. io_putchar('\n');
  266. cpuid(1, 0, &eax, &ebx, &ecx, &edx);
  267. ebx1 = ebx;
  268. ecx1 = ecx;
  269. edx1 = edx;
  270. parse_family_model(eax, &family, &model, &stepping);
  271. io_putstr("family ");
  272. put_uint(family);
  273. io_putstr(" model ");
  274. put_uint(model);
  275. io_putstr(" stepping ");
  276. put_uint(stepping);
  277. io_putchar('\n');
  278. if (max_ext_leaf >= 0x80000004u) {
  279. for (i = 0; i < 48; i++)
  280. brand[i] = '\0';
  281. for (i = 0; i < 3; i++) {
  282. unsigned int *dst = (unsigned int *)(brand + i * 16);
  283. cpuid(0x80000002u + i, 0, &eax, &ebx, &ecx, &edx);
  284. dst[0] = eax;
  285. dst[1] = ebx;
  286. dst[2] = ecx;
  287. dst[3] = edx;
  288. }
  289. brand[48] = '\0';
  290. trim_right_spaces(brand);
  291. if (brand[0] != '\0')
  292. has_brand = 1;
  293. }
  294. if (has_brand) {
  295. io_putstr("brand ");
  296. io_putstr(brand);
  297. io_putchar('\n');
  298. }
  299. /* Logical processors package-wide from leaf 1 EBX[23:16]. */
  300. logical_cpus = (ebx1 >> 16) & 0xFF;
  301. if (logical_cpus == 0)
  302. logical_cpus = 1;
  303. /* Physical core count (best effort across vendors/generations). */
  304. if (max_leaf >= 4) {
  305. cpuid(4, 0, &eax, &ebx, &ecx, &edx);
  306. cores = ((eax >> 26) & 0x3F) + 1;
  307. }
  308. if (max_ext_leaf >= 0x80000008u) {
  309. cpuid(0x80000008u, 0, &eax, &ebx, &ecx, &edx);
  310. if ((ecx & 0xFFu) != 0)
  311. cores = (ecx & 0xFFu) + 1;
  312. }
  313. if (cores == 0)
  314. cores = 1;
  315. if (logical_cpus >= cores && cores != 0)
  316. threads_per_core = logical_cpus / cores;
  317. if (threads_per_core == 0)
  318. threads_per_core = 1;
  319. io_putstr("topology ");
  320. put_uint(logical_cpus);
  321. io_putstr(" logical, ");
  322. put_uint(cores);
  323. io_putstr(" cores, ");
  324. put_uint(threads_per_core);
  325. io_putstr(" threads/core\n");
  326. print_cpuid_frequency(max_leaf);
  327. start_ms = get_ticks_ms();
  328. t0 = read_tsc_low();
  329. do {
  330. __asm__ volatile ("hlt");
  331. end_ms = get_ticks_ms();
  332. } while (end_ms - start_ms < 200);
  333. t1 = read_tsc_low();
  334. delta_ms = end_ms - start_ms;
  335. delta_cycles = t1 - t0;
  336. if (delta_ms == 0)
  337. delta_ms = 1;
  338. /* MHz with one decimal: cycles / (ms * 100). */
  339. mhz_x10 = delta_cycles / (delta_ms * 100);
  340. io_putstr("clock tsc ");
  341. put_uint(mhz_x10 / 10);
  342. io_putchar('.');
  343. put_uint(mhz_x10 % 10);
  344. io_putstr(" MHz (sampled)\n");
  345. print_features_leaf1(ecx1, edx1);
  346. print_features_leaf7(max_leaf);
  347. print_features_ext(max_ext_leaf);
  348. }
  349. static void print_power_usage(void)
  350. {
  351. io_putstr("usage:\n");
  352. io_putstr(" power info\n");
  353. io_putstr(" power set low (deep C-state, lowest power)\n");
  354. io_putstr(" power set balanced (moderate C-state)\n");
  355. io_putstr(" power set max (C1 only, max performance)\n");
  356. }
  357. static void detect_cpu(char vendor[13], unsigned int *max_leaf,
  358. unsigned int *eax1, unsigned int *ecx1, unsigned int *edx1,
  359. unsigned int *family, unsigned int *model, unsigned int *stepping)
  360. {
  361. unsigned int ebx, ecx, edx;
  362. cpuid(0, 0, max_leaf, &ebx, &ecx, &edx);
  363. ((unsigned int *)vendor)[0] = ebx;
  364. ((unsigned int *)vendor)[1] = edx;
  365. ((unsigned int *)vendor)[2] = ecx;
  366. vendor[12] = '\0';
  367. cpuid(1, 0, eax1, &ebx, ecx1, edx1);
  368. parse_family_model(*eax1, family, model, stepping);
  369. }
  370. static int intel_has_platform_info(unsigned int family, unsigned int model)
  371. {
  372. if (family != 6)
  373. return 0;
  374. if (model >= 0x1A)
  375. return 1;
  376. return 0;
  377. }
  378. static int intel_has_epb(unsigned int eax6)
  379. {
  380. return (eax6 & (1u << 3)) ? 1 : 0;
  381. }
  382. static void power_info_intel(unsigned int family, unsigned int model, unsigned int ecx1, unsigned int edx1)
  383. {
  384. unsigned int lo = 0, hi = 0;
  385. unsigned int target_ratio = 0, current_ratio = 0;
  386. unsigned int min_ratio = 0, max_ratio = 0;
  387. unsigned int eax6, ebx6, ecx6, edx6;
  388. int has_hwp = 0;
  389. int hwp_enabled = 0;
  390. int has_platinfo = 0;
  391. int has_epb = 0;
  392. if ((edx1 & (1u << 5)) == 0) {
  393. io_putstr("intel: no MSR feature bit\n");
  394. return;
  395. }
  396. if ((ecx1 & (1u << 7)) == 0)
  397. io_putstr("intel: EIST not advertised\n");
  398. if (family != 6 && family != 15) {
  399. io_putstr("intel: family not supported for perf ctl\n");
  400. return;
  401. }
  402. cpuid(6, 0, &eax6, &ebx6, &ecx6, &edx6);
  403. (void)ebx6;
  404. (void)ecx6;
  405. (void)edx6;
  406. has_hwp = (eax6 & (1u << 7)) ? 1 : 0;
  407. has_epb = intel_has_epb(eax6);
  408. has_platinfo = intel_has_platform_info(family, model);
  409. if (has_platinfo) {
  410. rdmsr_u32(MSR_IA32_PLATFORM_INFO, &lo, &hi);
  411. min_ratio = (hi >> 8) & 0xFFu;
  412. max_ratio = (lo >> 8) & 0xFFu;
  413. }
  414. rdmsr_u32(MSR_IA32_PERF_STATUS, &lo, &hi);
  415. current_ratio = (lo >> 8) & 0xFF;
  416. rdmsr_u32(MSR_IA32_PERF_CTL, &lo, &hi);
  417. target_ratio = (lo >> 8) & 0xFF;
  418. if (has_hwp) {
  419. rdmsr_u32(MSR_IA32_PM_ENABLE, &lo, &hi);
  420. hwp_enabled = (lo & 1u) ? 1 : 0;
  421. }
  422. io_putstr("intel ratio current ");
  423. put_uint(current_ratio);
  424. io_putstr(" target ");
  425. put_uint(target_ratio);
  426. io_putchar('\n');
  427. if (has_platinfo) {
  428. io_putstr("intel ratio range ");
  429. put_uint(min_ratio);
  430. io_putstr("..");
  431. put_uint(max_ratio);
  432. io_putchar('\n');
  433. }
  434. if (has_hwp) {
  435. io_putstr("intel hwp ");
  436. io_putstr(hwp_enabled ? "enabled\n" : "disabled\n");
  437. if (hwp_enabled) {
  438. rdmsr_u32(MSR_IA32_HWP_REQUEST, &lo, &hi);
  439. io_putstr("intel hwp req min ");
  440. put_uint(lo & 0xFFu);
  441. io_putstr(" max ");
  442. put_uint((lo >> 8) & 0xFFu);
  443. io_putstr(" des ");
  444. put_uint((lo >> 16) & 0xFFu);
  445. io_putstr(" epp ");
  446. put_uint((lo >> 24) & 0xFFu);
  447. io_putchar('\n');
  448. rdmsr_u32(MSR_IA32_HWP_CAPABILITIES, &lo, &hi);
  449. io_putstr("intel hwp cap low ");
  450. put_uint(lo & 0xFFu);
  451. io_putstr(" high ");
  452. put_uint((lo >> 8) & 0xFFu);
  453. io_putstr(" guar ");
  454. put_uint((lo >> 16) & 0xFFu);
  455. io_putstr(" eff ");
  456. put_uint((lo >> 24) & 0xFFu);
  457. io_putchar('\n');
  458. }
  459. }
  460. if (has_epb) {
  461. rdmsr_u32(MSR_IA32_ENERGY_PERF_BIAS, &lo, &hi);
  462. io_putstr("intel epb ");
  463. put_uint(lo & 0xFu);
  464. io_putchar('\n');
  465. }
  466. io_putstr("intel family ");
  467. put_uint(family);
  468. io_putstr(" model ");
  469. put_uint(model);
  470. io_putchar('\n');
  471. }
  472. static int power_set_intel(const char *profile, unsigned int family, unsigned int model, unsigned int ecx1, unsigned int edx1)
  473. {
  474. unsigned int lo = 0, hi = 0;
  475. unsigned int current_ratio, target_ratio;
  476. unsigned int min_ratio = 8, max_ratio = 63;
  477. unsigned int eax6, ebx6, ecx6, edx6;
  478. int has_hwp = 0;
  479. int hwp_enabled = 0;
  480. int has_platinfo = 0;
  481. if ((edx1 & (1u << 5)) == 0 || (ecx1 & (1u << 7)) == 0) {
  482. io_putstr("intel: missing MSR/EIST support\n");
  483. return -1;
  484. }
  485. if (family != 6 && family != 15) {
  486. io_putstr("intel: unsupported family for write path\n");
  487. return -1;
  488. }
  489. cpuid(6, 0, &eax6, &ebx6, &ecx6, &edx6);
  490. (void)ebx6;
  491. (void)ecx6;
  492. (void)edx6;
  493. has_hwp = (eax6 & (1u << 7)) ? 1 : 0;
  494. has_platinfo = intel_has_platform_info(family, model);
  495. if (has_platinfo) {
  496. rdmsr_u32(MSR_IA32_PLATFORM_INFO, &lo, &hi);
  497. min_ratio = (hi >> 8) & 0xFFu;
  498. max_ratio = (lo >> 8) & 0xFFu;
  499. if (min_ratio == 0 || max_ratio == 0 || min_ratio > max_ratio) {
  500. min_ratio = 8;
  501. max_ratio = 63;
  502. }
  503. }
  504. if (has_hwp) {
  505. rdmsr_u32(MSR_IA32_PM_ENABLE, &lo, &hi);
  506. hwp_enabled = (lo & 1u) ? 1 : 0;
  507. }
  508. rdmsr_u32(MSR_IA32_PERF_STATUS, &lo, &hi);
  509. current_ratio = (lo >> 8) & 0xFF;
  510. target_ratio = current_ratio < min_ratio ? min_ratio : current_ratio;
  511. if (target_ratio > max_ratio)
  512. target_ratio = max_ratio;
  513. if (streq(profile, "low")) {
  514. target_ratio = min_ratio;
  515. } else if (streq(profile, "balanced")) {
  516. target_ratio = (min_ratio + max_ratio) / 2;
  517. } else if (streq(profile, "max")) {
  518. target_ratio = max_ratio;
  519. } else {
  520. io_putstr("power: invalid profile\n");
  521. return -1;
  522. }
  523. if (has_hwp && hwp_enabled) {
  524. unsigned int minp = min_ratio;
  525. unsigned int maxp = max_ratio;
  526. unsigned int des = target_ratio;
  527. unsigned int epp = 0x80u;
  528. if (streq(profile, "low"))
  529. epp = 0xC0u;
  530. else if (streq(profile, "balanced"))
  531. epp = 0x80u;
  532. else if (streq(profile, "max"))
  533. epp = 0x00u;
  534. lo = (minp & 0xFFu) |
  535. ((maxp & 0xFFu) << 8) |
  536. ((des & 0xFFu) << 16) |
  537. ((epp & 0xFFu) << 24);
  538. wrmsr_u32(MSR_IA32_HWP_REQUEST, lo, 0);
  539. io_putstr("intel hwp profile ");
  540. io_putstr(profile);
  541. io_putstr(" set (min ");
  542. put_uint(minp);
  543. io_putstr(" max ");
  544. put_uint(maxp);
  545. io_putstr(" des ");
  546. put_uint(des);
  547. io_putstr(" epp ");
  548. put_uint(epp);
  549. io_putstr(")\n");
  550. return 0;
  551. }
  552. rdmsr_u32(MSR_IA32_PERF_CTL, &lo, &hi);
  553. lo &= ~0xFF00u;
  554. lo |= (target_ratio & 0xFFu) << 8;
  555. wrmsr_u32(MSR_IA32_PERF_CTL, lo, hi);
  556. io_putstr("intel target ratio set ");
  557. put_uint(target_ratio);
  558. io_putchar('\n');
  559. return 0;
  560. }
  561. static void power_info_amd(unsigned int family, unsigned int model, unsigned int edx1)
  562. {
  563. unsigned int lo = 0, hi = 0;
  564. if ((edx1 & (1u << 5)) == 0) {
  565. io_putstr("amd: no MSR feature bit\n");
  566. return;
  567. }
  568. if (family < 0x10) {
  569. io_putstr("amd: family too old for p-state ctl\n");
  570. return;
  571. }
  572. rdmsr_u32(MSR_AMD_PSTATE_STATUS, &lo, &hi);
  573. io_putstr("amd pstate current ");
  574. put_uint(lo & 0x7u);
  575. io_putstr(" raw 0x");
  576. put_hex32(lo);
  577. io_putchar('\n');
  578. io_putstr("amd family ");
  579. put_uint(family);
  580. io_putstr(" model ");
  581. put_uint(model);
  582. io_putchar('\n');
  583. }
  584. static int power_set_amd(const char *profile, unsigned int family, unsigned int edx1)
  585. {
  586. unsigned int lo = 0, hi = 0;
  587. unsigned int pstate = 0;
  588. if ((edx1 & (1u << 5)) == 0) {
  589. io_putstr("amd: no MSR support\n");
  590. return -1;
  591. }
  592. if (family < 0x10) {
  593. io_putstr("amd: unsupported family for write path\n");
  594. return -1;
  595. }
  596. if (streq(profile, "max")) {
  597. pstate = 0;
  598. } else if (streq(profile, "balanced")) {
  599. pstate = 1;
  600. } else if (streq(profile, "low")) {
  601. pstate = 2;
  602. } else {
  603. io_putstr("power: invalid profile\n");
  604. return -1;
  605. }
  606. rdmsr_u32(MSR_AMD_PSTATE_CTL, &lo, &hi);
  607. lo &= ~0x7u;
  608. lo |= pstate;
  609. wrmsr_u32(MSR_AMD_PSTATE_CTL, lo, hi);
  610. io_putstr("amd target pstate set ");
  611. put_uint(pstate);
  612. io_putchar('\n');
  613. return 0;
  614. }
  615. void program_power_main(const char *args)
  616. {
  617. char vendor[13];
  618. char local[40];
  619. char command[8];
  620. char profile[16];
  621. unsigned int max_leaf, eax1, ecx1, edx1, family, model, stepping;
  622. unsigned int i = 0, j = 0;
  623. int is_set = 0;
  624. unsigned int cstate_level;
  625. detect_cpu(vendor, &max_leaf, &eax1, &ecx1, &edx1, &family, &model, &stepping);
  626. (void)eax1;
  627. (void)max_leaf;
  628. while (args && args[i] && i + 1 < sizeof(local)) {
  629. local[i] = args[i];
  630. i++;
  631. }
  632. local[i] = '\0';
  633. i = 0;
  634. while (local[i] == ' ')
  635. i++;
  636. j = 0;
  637. while (local[i] && local[i] != ' ' && j + 1 < sizeof(command))
  638. command[j++] = local[i++];
  639. command[j] = '\0';
  640. while (local[i] == ' ')
  641. i++;
  642. j = 0;
  643. while (local[i] && local[i] != ' ' && j + 1 < sizeof(profile))
  644. profile[j++] = local[i++];
  645. profile[j] = '\0';
  646. if (command[0] == '\0' || streq(command, "info")) {
  647. io_putstr("power vendor ");
  648. io_putstr(vendor);
  649. io_putstr(" family ");
  650. put_uint(family);
  651. io_putstr(" model ");
  652. put_uint(model);
  653. io_putstr(" stepping ");
  654. put_uint(stepping);
  655. io_putchar('\n');
  656. io_putstr("idle mode ");
  657. if (idle_has_mwait())
  658. io_putstr("mwait");
  659. else
  660. io_putstr("hlt");
  661. io_putstr(" cstate C");
  662. put_uint(idle_get_cstate());
  663. io_putchar('\n');
  664. if (streq(vendor, "GenuineIntel"))
  665. power_info_intel(family, model, ecx1, edx1);
  666. else if (streq(vendor, "AuthenticAMD"))
  667. power_info_amd(family, model, edx1);
  668. else
  669. io_putstr("power: vendor backend unavailable\n");
  670. print_power_usage();
  671. return;
  672. }
  673. if (streq(command, "set"))
  674. is_set = 1;
  675. if (!is_set || profile[0] == '\0') {
  676. print_power_usage();
  677. return;
  678. }
  679. if (str_starts_with(profile, "low") == 0 &&
  680. str_starts_with(profile, "balanced") == 0 &&
  681. str_starts_with(profile, "max") == 0) {
  682. io_putstr("power: expected low|balanced|max\n");
  683. return;
  684. }
  685. if (streq(profile, "low")) {
  686. cstate_level = 3;
  687. } else if (streq(profile, "balanced")) {
  688. cstate_level = 2;
  689. } else {
  690. cstate_level = 0;
  691. }
  692. idle_set_cstate(cstate_level);
  693. io_putstr("idle cstate set C");
  694. put_uint(cstate_level);
  695. io_putchar('\n');
  696. if (streq(vendor, "GenuineIntel")) {
  697. if (power_set_intel(profile, family, model, ecx1, edx1) < 0)
  698. io_putstr("power: intel p-state control unavailable\n");
  699. } else if (streq(vendor, "AuthenticAMD")) {
  700. if (power_set_amd(profile, family, edx1) < 0)
  701. io_putstr("power: amd p-state control unavailable\n");
  702. }
  703. }