1 // SPDX-License-Identifier: GPL-2.0-only
3 * x86_energy_perf_policy -- set the energy versus performance
4 * policy preference bias on recent X86 processors.
7 * Copyright (c) 2010 - 2017 Intel Corporation.
8 * Len Brown <len.brown@intel.com>
15 #include <sys/types.h>
18 #include <sys/resource.h>
30 #define OPTARG_NORMAL (INT_MAX - 1)
31 #define OPTARG_POWER (INT_MAX - 2)
32 #define OPTARG_BALANCE_POWER (INT_MAX - 3)
33 #define OPTARG_BALANCE_PERFORMANCE (INT_MAX - 4)
34 #define OPTARG_PERFORMANCE (INT_MAX - 5)
37 unsigned char highest;
38 unsigned char guaranteed;
39 unsigned char efficient;
43 struct msr_hwp_request {
44 unsigned char hwp_min;
45 unsigned char hwp_max;
46 unsigned char hwp_desired;
47 unsigned char hwp_epp;
48 unsigned int hwp_window;
49 unsigned char hwp_use_pkg;
57 unsigned char update_epb;
58 unsigned long long new_epb;
59 unsigned char turbo_is_enabled;
60 unsigned char update_turbo;
61 unsigned char turbo_update_value;
62 unsigned char update_hwp_epp;
63 unsigned char update_hwp_min;
64 unsigned char update_hwp_max;
65 unsigned char update_hwp_desired;
66 unsigned char update_hwp_window;
67 unsigned char update_hwp_use_pkg;
68 unsigned char update_hwp_enable;
69 #define hwp_update_enabled() (update_hwp_enable | update_hwp_epp | update_hwp_max | update_hwp_min | update_hwp_desired | update_hwp_window | update_hwp_use_pkg)
72 #define MAX_PACKAGES 64
73 unsigned int first_cpu_in_pkg[MAX_PACKAGES];
74 unsigned long long pkg_present_set;
75 unsigned long long pkg_selected_set;
76 cpu_set_t *cpu_present_set;
77 cpu_set_t *cpu_selected_set;
82 char *proc_stat = "/proc/stat";
84 unsigned int has_epb; /* MSR_IA32_ENERGY_PERF_BIAS */
85 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
86 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
87 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
88 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
89 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
90 unsigned int has_hwp_request_pkg; /* IA32_HWP_REQUEST_PKG */
92 unsigned int bdx_highest_ratio;
94 #define PATH_TO_CPU "/sys/devices/system/cpu/"
95 #define SYSFS_PATH_MAX 255
98 * maintain compatibility with original implementation, but don't document it:
102 fprintf(stderr, "%s [options] [scope][field value]\n", progname);
103 fprintf(stderr, "scope: --cpu cpu-list [--hwp-use-pkg #] | --pkg pkg-list\n");
104 fprintf(stderr, "field: --all | --epb | --hwp-epp | --hwp-min | --hwp-max | --hwp-desired\n");
105 fprintf(stderr, "other: --hwp-enable | --turbo-enable (0 | 1) | --help | --force\n");
107 "value: ( # | \"normal\" | \"performance\" | \"balance-performance\" | \"balance-power\"| \"power\")\n");
108 fprintf(stderr, "--hwp-window usec\n");
110 fprintf(stderr, "Specify only Energy Performance BIAS (legacy usage):\n");
111 fprintf(stderr, "%s: [-c cpu] [-v] (-r | policy-value )\n", progname);
117 * If bdx_highest_ratio is set,
118 * then we must translate between MSR format and simple ratio
119 * used on the cmdline.
121 int ratio_2_msr_perf(int ratio)
125 if (!bdx_highest_ratio)
128 msr_perf = ratio * 255 / bdx_highest_ratio;
131 fprintf(stderr, "%d = ratio_to_msr_perf(%d)\n", msr_perf, ratio);
135 int msr_perf_2_ratio(int msr_perf)
140 if (!bdx_highest_ratio)
143 d = (double)msr_perf * (double) bdx_highest_ratio / 255.0;
144 d = d + 0.5; /* round */
148 fprintf(stderr, "%d = msr_perf_ratio(%d) {%f}\n", ratio, msr_perf, d);
152 int parse_cmdline_epb(int i)
155 errx(1, "EPB not enabled on this platform");
161 return ENERGY_PERF_BIAS_POWERSAVE;
162 case OPTARG_BALANCE_POWER:
163 return ENERGY_PERF_BIAS_BALANCE_POWERSAVE;
165 return ENERGY_PERF_BIAS_NORMAL;
166 case OPTARG_BALANCE_PERFORMANCE:
167 return ENERGY_PERF_BIAS_BALANCE_PERFORMANCE;
168 case OPTARG_PERFORMANCE:
169 return ENERGY_PERF_BIAS_PERFORMANCE;
171 if (i < 0 || i > ENERGY_PERF_BIAS_POWERSAVE)
172 errx(1, "--epb must be from 0 to 15");
176 #define HWP_CAP_LOWEST 0
177 #define HWP_CAP_HIGHEST 255
180 * "performance" changes hwp_min to cap.highest
181 * All others leave it at cap.lowest
183 int parse_cmdline_hwp_min(int i)
189 case OPTARG_BALANCE_POWER:
191 case OPTARG_BALANCE_PERFORMANCE:
192 return HWP_CAP_LOWEST;
193 case OPTARG_PERFORMANCE:
194 return HWP_CAP_HIGHEST;
199 * "power" changes hwp_max to cap.lowest
200 * All others leave it at cap.highest
202 int parse_cmdline_hwp_max(int i)
208 return HWP_CAP_LOWEST;
210 case OPTARG_BALANCE_POWER:
211 case OPTARG_BALANCE_PERFORMANCE:
212 case OPTARG_PERFORMANCE:
213 return HWP_CAP_HIGHEST;
218 * for --hwp-des, all strings leave it in autonomous mode
219 * If you want to change it, you need to explicitly pick a value
221 int parse_cmdline_hwp_desired(int i)
223 update_hwp_desired = 1;
227 case OPTARG_BALANCE_POWER:
228 case OPTARG_BALANCE_PERFORMANCE:
230 case OPTARG_PERFORMANCE:
231 return 0; /* autonomous */
236 int parse_cmdline_hwp_window(int i)
238 unsigned int exponent;
240 update_hwp_window = 1;
244 case OPTARG_BALANCE_POWER:
246 case OPTARG_BALANCE_PERFORMANCE:
247 case OPTARG_PERFORMANCE:
250 if (i < 0 || i > 1270000000) {
251 fprintf(stderr, "--hwp-window: 0 for auto; 1 - 1270000000 usec for window duration\n");
254 for (exponent = 0; ; ++exponent) {
256 printf("%d 10^%d\n", i, exponent);
264 fprintf(stderr, "%d*10^%d: 0x%x\n", i, exponent, (exponent << 7) | i);
266 return (exponent << 7) | i;
268 int parse_cmdline_hwp_epp(int i)
274 return HWP_EPP_POWERSAVE;
275 case OPTARG_BALANCE_POWER:
276 return HWP_EPP_BALANCE_POWERSAVE;
278 case OPTARG_BALANCE_PERFORMANCE:
279 return HWP_EPP_BALANCE_PERFORMANCE;
280 case OPTARG_PERFORMANCE:
281 return HWP_EPP_PERFORMANCE;
283 if (i < 0 || i > 0xff) {
284 fprintf(stderr, "--hwp-epp must be from 0 to 0xff\n");
289 int parse_cmdline_turbo(int i)
297 case OPTARG_BALANCE_POWER:
298 case OPTARG_BALANCE_PERFORMANCE:
299 case OPTARG_PERFORMANCE:
302 if (i < 0 || i > 1) {
303 fprintf(stderr, "--turbo-enable: 1 to enable, 0 to disable\n");
309 int parse_optarg_string(char *s)
314 if (!strncmp(s, "default", 7))
315 return OPTARG_NORMAL;
317 if (!strncmp(s, "normal", 6))
318 return OPTARG_NORMAL;
320 if (!strncmp(s, "power", 9))
323 if (!strncmp(s, "balance-power", 17))
324 return OPTARG_BALANCE_POWER;
326 if (!strncmp(s, "balance-performance", 19))
327 return OPTARG_BALANCE_PERFORMANCE;
329 if (!strncmp(s, "performance", 11))
330 return OPTARG_PERFORMANCE;
332 i = strtol(s, &endptr, 0);
334 fprintf(stderr, "no digits in \"%s\"\n", s);
337 if (i == LONG_MIN || i == LONG_MAX)
341 errx(-1, "%d (0x%x) must be < 256", i, i);
344 errx(-1, "%d (0x%x) must be >= 0", i, i);
348 void parse_cmdline_all(char *s)
351 update_hwp_enable = 1;
352 req_update.hwp_min = parse_cmdline_hwp_min(parse_optarg_string(s));
353 req_update.hwp_max = parse_cmdline_hwp_max(parse_optarg_string(s));
354 req_update.hwp_epp = parse_cmdline_hwp_epp(parse_optarg_string(s));
356 new_epb = parse_cmdline_epb(parse_optarg_string(s));
357 turbo_update_value = parse_cmdline_turbo(parse_optarg_string(s));
358 req_update.hwp_desired = parse_cmdline_hwp_desired(parse_optarg_string(s));
359 req_update.hwp_window = parse_cmdline_hwp_window(parse_optarg_string(s));
362 void validate_cpu_selected_set(void)
366 if (CPU_COUNT_S(cpu_setsize, cpu_selected_set) == 0)
367 errx(0, "no CPUs requested");
369 for (cpu = 0; cpu <= max_cpu_num; ++cpu) {
370 if (CPU_ISSET_S(cpu, cpu_setsize, cpu_selected_set))
371 if (!CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
372 errx(1, "Requested cpu% is not present", cpu);
376 void parse_cmdline_cpu(char *s)
381 if (pkg_selected_set) {
383 errx(1, "--cpu | --pkg");
385 cpu_selected_set = CPU_ALLOC((max_cpu_num + 1));
386 if (cpu_selected_set == NULL)
387 err(1, "cpu_selected_set");
388 CPU_ZERO_S(cpu_setsize, cpu_selected_set);
390 for (startp = s; startp && *startp;) {
392 if (*startp == ',') {
397 if (*startp == '-') {
401 end_cpu = strtol(startp, &endp, 10);
405 while (cpu <= end_cpu) {
406 if (cpu > max_cpu_num)
407 errx(1, "Requested cpu%d exceeds max cpu%d", cpu, max_cpu_num);
408 CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
415 if (strncmp(startp, "all", 3) == 0) {
416 for (cpu = 0; cpu <= max_cpu_num; cpu += 1) {
417 if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
418 CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
424 /* "--cpu even" is not documented */
425 if (strncmp(startp, "even", 4) == 0) {
426 for (cpu = 0; cpu <= max_cpu_num; cpu += 2) {
427 if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
428 CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
435 /* "--cpu odd" is not documented */
436 if (strncmp(startp, "odd", 3) == 0) {
437 for (cpu = 1; cpu <= max_cpu_num; cpu += 2) {
438 if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
439 CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
446 cpu = strtol(startp, &endp, 10);
448 errx(1, "--cpu cpu-set: confused by '%s'", startp);
449 if (cpu > max_cpu_num)
450 errx(1, "Requested cpu%d exceeds max cpu%d", cpu, max_cpu_num);
451 CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
455 validate_cpu_selected_set();
459 void parse_cmdline_pkg(char *s)
464 if (cpu_selected_set) {
466 errx(1, "--pkg | --cpu");
468 pkg_selected_set = 0;
470 for (startp = s; startp && *startp;) {
472 if (*startp == ',') {
477 if (*startp == '-') {
481 end_pkg = strtol(startp, &endp, 10);
485 while (pkg <= end_pkg) {
486 if (pkg > max_pkg_num)
487 errx(1, "Requested pkg%d exceeds max pkg%d", pkg, max_pkg_num);
488 pkg_selected_set |= 1 << pkg;
495 if (strncmp(startp, "all", 3) == 0) {
496 pkg_selected_set = pkg_present_set;
500 pkg = strtol(startp, &endp, 10);
501 if (pkg > max_pkg_num)
502 errx(1, "Requested pkg%d Exceeds max pkg%d", pkg, max_pkg_num);
503 pkg_selected_set |= 1 << pkg;
508 void for_packages(unsigned long long pkg_set, int (func)(int))
512 for (pkg_num = 0; pkg_num <= max_pkg_num; ++pkg_num) {
513 if (pkg_set & (1UL << pkg_num))
518 void print_version(void)
520 printf("x86_energy_perf_policy 17.05.11 (C) Len Brown <len.brown@intel.com>\n");
523 void cmdline(int argc, char **argv)
526 int option_index = 0;
528 static struct option long_options[] = {
529 {"all", required_argument, 0, 'a'},
530 {"cpu", required_argument, 0, 'c'},
531 {"pkg", required_argument, 0, 'p'},
532 {"debug", no_argument, 0, 'd'},
533 {"hwp-desired", required_argument, 0, 'D'},
534 {"epb", required_argument, 0, 'B'},
535 {"force", no_argument, 0, 'f'},
536 {"hwp-enable", no_argument, 0, 'e'},
537 {"help", no_argument, 0, 'h'},
538 {"hwp-epp", required_argument, 0, 'P'},
539 {"hwp-min", required_argument, 0, 'm'},
540 {"hwp-max", required_argument, 0, 'M'},
541 {"read", no_argument, 0, 'r'},
542 {"turbo-enable", required_argument, 0, 't'},
543 {"hwp-use-pkg", required_argument, 0, 'u'},
544 {"version", no_argument, 0, 'v'},
545 {"hwp-window", required_argument, 0, 'w'},
551 while ((opt = getopt_long_only(argc, argv, "+a:c:dD:E:e:f:m:M:rt:u:vw:",
552 long_options, &option_index)) != -1) {
555 parse_cmdline_all(optarg);
558 new_epb = parse_cmdline_epb(parse_optarg_string(optarg));
561 parse_cmdline_cpu(optarg);
564 update_hwp_enable = 1;
577 req_update.hwp_desired = parse_cmdline_hwp_desired(parse_optarg_string(optarg));
580 req_update.hwp_min = parse_cmdline_hwp_min(parse_optarg_string(optarg));
583 req_update.hwp_max = parse_cmdline_hwp_max(parse_optarg_string(optarg));
586 parse_cmdline_pkg(optarg);
589 req_update.hwp_epp = parse_cmdline_hwp_epp(parse_optarg_string(optarg));
592 /* v1 used -r to specify read-only mode, now the default */
595 turbo_update_value = parse_cmdline_turbo(parse_optarg_string(optarg));
598 update_hwp_use_pkg++;
599 if (atoi(optarg) == 0)
600 req_update.hwp_use_pkg = 0;
602 req_update.hwp_use_pkg = 1;
609 req_update.hwp_window = parse_cmdline_hwp_window(parse_optarg_string(optarg));
616 * v1 allowed "performance"|"normal"|"power" with no policy specifier
617 * to update BIAS. Continue to support that, even though no longer documented.
619 if (argc == optind + 1)
620 new_epb = parse_cmdline_epb(parse_optarg_string(argv[optind]));
622 if (argc > optind + 1) {
623 fprintf(stderr, "stray parameter '%s'\n", argv[optind + 1]);
629 * Open a file, and exit on failure
631 FILE *fopen_or_die(const char *path, const char *mode)
633 FILE *filep = fopen(path, "r");
636 err(1, "%s: open failed", path);
640 void err_on_hypervisor(void)
643 char *flags, *hypervisor;
646 /* On VMs /proc/cpuinfo contains a "flags" entry for hypervisor */
647 cpuinfo = fopen_or_die("/proc/cpuinfo", "ro");
649 buffer = malloc(4096);
652 err(-ENOMEM, "buffer malloc fail");
655 if (!fread(buffer, 1024, 1, cpuinfo)) {
658 err(1, "Reading /proc/cpuinfo failed");
661 flags = strstr(buffer, "flags");
663 fseek(cpuinfo, flags - buffer, SEEK_SET);
664 if (!fgets(buffer, 4096, cpuinfo)) {
667 err(1, "Reading /proc/cpuinfo failed");
671 hypervisor = strstr(buffer, "hypervisor");
677 "not supported on this virtual machine");
680 int get_msr(int cpu, int offset, unsigned long long *msr)
686 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
687 fd = open(pathname, O_RDONLY);
689 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
691 retval = pread(fd, msr, sizeof(*msr), offset);
692 if (retval != sizeof(*msr)) {
694 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
698 fprintf(stderr, "get_msr(cpu%d, 0x%X, 0x%llX)\n", cpu, offset, *msr);
704 int put_msr(int cpu, int offset, unsigned long long new_msr)
710 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
711 fd = open(pathname, O_RDWR);
713 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
715 retval = pwrite(fd, &new_msr, sizeof(new_msr), offset);
716 if (retval != sizeof(new_msr))
717 err(-2, "pwrite(cpu%d, offset 0x%x, 0x%llx) = %d", cpu, offset, new_msr, retval);
722 fprintf(stderr, "put_msr(cpu%d, 0x%X, 0x%llX)\n", cpu, offset, new_msr);
727 static unsigned int read_sysfs(const char *path, char *buf, size_t buflen)
732 fd = open(path, O_RDONLY);
736 numread = read(fd, buf, buflen - 1);
745 return (unsigned int) numread;
748 static unsigned int write_sysfs(const char *path, char *buf, size_t buflen)
753 fd = open(path, O_WRONLY);
757 numwritten = write(fd, buf, buflen - 1);
758 if (numwritten < 1) {
759 perror("write failed\n");
766 return (unsigned int) numwritten;
769 void print_hwp_cap(int cpu, struct msr_hwp_cap *cap, char *str)
772 printf("cpu%d: ", cpu);
774 printf("HWP_CAP: low %d eff %d guar %d high %d\n",
775 cap->lowest, cap->efficient, cap->guaranteed, cap->highest);
777 void read_hwp_cap(int cpu, struct msr_hwp_cap *cap, unsigned int msr_offset)
779 unsigned long long msr;
781 get_msr(cpu, msr_offset, &msr);
783 cap->highest = msr_perf_2_ratio(HWP_HIGHEST_PERF(msr));
784 cap->guaranteed = msr_perf_2_ratio(HWP_GUARANTEED_PERF(msr));
785 cap->efficient = msr_perf_2_ratio(HWP_MOSTEFFICIENT_PERF(msr));
786 cap->lowest = msr_perf_2_ratio(HWP_LOWEST_PERF(msr));
789 void print_hwp_request(int cpu, struct msr_hwp_request *h, char *str)
792 printf("cpu%d: ", cpu);
797 printf("HWP_REQ: min %d max %d des %d epp %d window 0x%x (%d*10^%dus) use_pkg %d\n",
798 h->hwp_min, h->hwp_max, h->hwp_desired, h->hwp_epp,
799 h->hwp_window, h->hwp_window & 0x7F, (h->hwp_window >> 7) & 0x7, h->hwp_use_pkg);
801 void print_hwp_request_pkg(int pkg, struct msr_hwp_request *h, char *str)
803 printf("pkg%d: ", pkg);
808 printf("HWP_REQ_PKG: min %d max %d des %d epp %d window 0x%x (%d*10^%dus)\n",
809 h->hwp_min, h->hwp_max, h->hwp_desired, h->hwp_epp,
810 h->hwp_window, h->hwp_window & 0x7F, (h->hwp_window >> 7) & 0x7);
812 void read_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msr_offset)
814 unsigned long long msr;
816 get_msr(cpu, msr_offset, &msr);
818 hwp_req->hwp_min = msr_perf_2_ratio((((msr) >> 0) & 0xff));
819 hwp_req->hwp_max = msr_perf_2_ratio((((msr) >> 8) & 0xff));
820 hwp_req->hwp_desired = msr_perf_2_ratio((((msr) >> 16) & 0xff));
821 hwp_req->hwp_epp = (((msr) >> 24) & 0xff);
822 hwp_req->hwp_window = (((msr) >> 32) & 0x3ff);
823 hwp_req->hwp_use_pkg = (((msr) >> 42) & 0x1);
826 void write_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msr_offset)
828 unsigned long long msr = 0;
831 printf("cpu%d: requesting min %d max %d des %d epp %d window 0x%0x use_pkg %d\n",
832 cpu, hwp_req->hwp_min, hwp_req->hwp_max,
833 hwp_req->hwp_desired, hwp_req->hwp_epp,
834 hwp_req->hwp_window, hwp_req->hwp_use_pkg);
836 msr |= HWP_MIN_PERF(ratio_2_msr_perf(hwp_req->hwp_min));
837 msr |= HWP_MAX_PERF(ratio_2_msr_perf(hwp_req->hwp_max));
838 msr |= HWP_DESIRED_PERF(ratio_2_msr_perf(hwp_req->hwp_desired));
839 msr |= HWP_ENERGY_PERF_PREFERENCE(hwp_req->hwp_epp);
840 msr |= HWP_ACTIVITY_WINDOW(hwp_req->hwp_window);
841 msr |= HWP_PACKAGE_CONTROL(hwp_req->hwp_use_pkg);
843 put_msr(cpu, msr_offset, msr);
846 static int get_epb(int cpu)
848 char path[SYSFS_PATH_MAX];
856 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
858 if (!read_sysfs(path, linebuf, 3))
861 val = strtol(linebuf, &endp, 0);
862 if (endp == linebuf || errno == ERANGE)
868 static int set_epb(int cpu, int val)
870 char path[SYSFS_PATH_MAX];
878 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
879 snprintf(linebuf, sizeof(linebuf), "%d", val);
881 ret = write_sysfs(path, linebuf, 3);
885 val = strtol(linebuf, &endp, 0);
886 if (endp == linebuf || errno == ERANGE)
892 int print_cpu_msrs(int cpu)
894 struct msr_hwp_request req;
895 struct msr_hwp_cap cap;
900 printf("cpu%d: EPB %u\n", cpu, (unsigned int) epb);
905 read_hwp_request(cpu, &req, MSR_HWP_REQUEST);
906 print_hwp_request(cpu, &req, "");
908 read_hwp_cap(cpu, &cap, MSR_HWP_CAPABILITIES);
909 print_hwp_cap(cpu, &cap, "");
914 int print_pkg_msrs(int pkg)
916 struct msr_hwp_request req;
917 unsigned long long msr;
922 read_hwp_request(first_cpu_in_pkg[pkg], &req, MSR_HWP_REQUEST_PKG);
923 print_hwp_request_pkg(pkg, &req, "");
925 if (has_hwp_notify) {
926 get_msr(first_cpu_in_pkg[pkg], MSR_HWP_INTERRUPT, &msr);
928 "pkg%d: MSR_HWP_INTERRUPT: 0x%08llx (Excursion_Min-%sabled, Guaranteed_Perf_Change-%sabled)\n",
930 ((msr) & 0x2) ? "EN" : "Dis",
931 ((msr) & 0x1) ? "EN" : "Dis");
933 get_msr(first_cpu_in_pkg[pkg], MSR_HWP_STATUS, &msr);
935 "pkg%d: MSR_HWP_STATUS: 0x%08llx (%sExcursion_Min, %sGuaranteed_Perf_Change)\n",
937 ((msr) & 0x4) ? "" : "No-",
938 ((msr) & 0x1) ? "" : "No-");
944 * Assumption: All HWP systems have 100 MHz bus clock
946 int ratio_2_sysfs_khz(int ratio)
948 int bclk_khz = 100 * 1000; /* 100,000 KHz = 100 MHz */
950 return ratio * bclk_khz;
953 * If HWP is enabled and cpufreq sysfs attribtes are present,
954 * then update sysfs, so that it will not become
955 * stale when we write to MSRs.
956 * (intel_pstate's max_perf_pct and min_perf_pct will follow cpufreq,
957 * so we don't have to touch that.)
959 void update_cpufreq_scaling_freq(int is_max, int cpu, unsigned int ratio)
966 sprintf(pathname, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
967 cpu, is_max ? "max" : "min");
969 fp = fopen(pathname, "w");
976 khz = ratio_2_sysfs_khz(ratio);
977 retval = fprintf(fp, "%d", khz);
982 printf("echo %d > %s\n", khz, pathname);
988 * We update all sysfs before updating any MSRs because of
989 * bugs in cpufreq/intel_pstate where the sysfs writes
990 * for a CPU may change the min/max values on other CPUS.
993 int update_sysfs(int cpu)
998 if (!hwp_update_enabled())
1001 if (access("/sys/devices/system/cpu/cpu0/cpufreq", F_OK))
1005 update_cpufreq_scaling_freq(0, cpu, req_update.hwp_min);
1008 update_cpufreq_scaling_freq(1, cpu, req_update.hwp_max);
1013 int verify_hwp_req_self_consistency(int cpu, struct msr_hwp_request *req)
1015 /* fail if min > max requested */
1016 if (req->hwp_min > req->hwp_max) {
1017 errx(1, "cpu%d: requested hwp-min %d > hwp_max %d",
1018 cpu, req->hwp_min, req->hwp_max);
1021 /* fail if desired > max requestd */
1022 if (req->hwp_desired && (req->hwp_desired > req->hwp_max)) {
1023 errx(1, "cpu%d: requested hwp-desired %d > hwp_max %d",
1024 cpu, req->hwp_desired, req->hwp_max);
1026 /* fail if desired < min requestd */
1027 if (req->hwp_desired && (req->hwp_desired < req->hwp_min)) {
1028 errx(1, "cpu%d: requested hwp-desired %d < requested hwp_min %d",
1029 cpu, req->hwp_desired, req->hwp_min);
1035 int check_hwp_request_v_hwp_capabilities(int cpu, struct msr_hwp_request *req, struct msr_hwp_cap *cap)
1037 if (update_hwp_max) {
1038 if (req->hwp_max > cap->highest)
1039 errx(1, "cpu%d: requested max %d > capabilities highest %d, use --force?",
1040 cpu, req->hwp_max, cap->highest);
1041 if (req->hwp_max < cap->lowest)
1042 errx(1, "cpu%d: requested max %d < capabilities lowest %d, use --force?",
1043 cpu, req->hwp_max, cap->lowest);
1046 if (update_hwp_min) {
1047 if (req->hwp_min > cap->highest)
1048 errx(1, "cpu%d: requested min %d > capabilities highest %d, use --force?",
1049 cpu, req->hwp_min, cap->highest);
1050 if (req->hwp_min < cap->lowest)
1051 errx(1, "cpu%d: requested min %d < capabilities lowest %d, use --force?",
1052 cpu, req->hwp_min, cap->lowest);
1055 if (update_hwp_min && update_hwp_max && (req->hwp_min > req->hwp_max))
1056 errx(1, "cpu%d: requested min %d > requested max %d",
1057 cpu, req->hwp_min, req->hwp_max);
1059 if (update_hwp_desired && req->hwp_desired) {
1060 if (req->hwp_desired > req->hwp_max)
1061 errx(1, "cpu%d: requested desired %d > requested max %d, use --force?",
1062 cpu, req->hwp_desired, req->hwp_max);
1063 if (req->hwp_desired < req->hwp_min)
1064 errx(1, "cpu%d: requested desired %d < requested min %d, use --force?",
1065 cpu, req->hwp_desired, req->hwp_min);
1066 if (req->hwp_desired < cap->lowest)
1067 errx(1, "cpu%d: requested desired %d < capabilities lowest %d, use --force?",
1068 cpu, req->hwp_desired, cap->lowest);
1069 if (req->hwp_desired > cap->highest)
1070 errx(1, "cpu%d: requested desired %d > capabilities highest %d, use --force?",
1071 cpu, req->hwp_desired, cap->highest);
1077 int update_hwp_request(int cpu)
1079 struct msr_hwp_request req;
1080 struct msr_hwp_cap cap;
1082 int msr_offset = MSR_HWP_REQUEST;
1084 read_hwp_request(cpu, &req, msr_offset);
1086 print_hwp_request(cpu, &req, "old: ");
1089 req.hwp_min = req_update.hwp_min;
1092 req.hwp_max = req_update.hwp_max;
1094 if (update_hwp_desired)
1095 req.hwp_desired = req_update.hwp_desired;
1097 if (update_hwp_window)
1098 req.hwp_window = req_update.hwp_window;
1101 req.hwp_epp = req_update.hwp_epp;
1103 req.hwp_use_pkg = req_update.hwp_use_pkg;
1105 read_hwp_cap(cpu, &cap, MSR_HWP_CAPABILITIES);
1107 print_hwp_cap(cpu, &cap, "");
1110 check_hwp_request_v_hwp_capabilities(cpu, &req, &cap);
1112 verify_hwp_req_self_consistency(cpu, &req);
1114 write_hwp_request(cpu, &req, msr_offset);
1117 read_hwp_request(cpu, &req, msr_offset);
1118 print_hwp_request(cpu, &req, "new: ");
1122 int update_hwp_request_pkg(int pkg)
1124 struct msr_hwp_request req;
1125 struct msr_hwp_cap cap;
1126 int cpu = first_cpu_in_pkg[pkg];
1128 int msr_offset = MSR_HWP_REQUEST_PKG;
1130 read_hwp_request(cpu, &req, msr_offset);
1132 print_hwp_request_pkg(pkg, &req, "old: ");
1135 req.hwp_min = req_update.hwp_min;
1138 req.hwp_max = req_update.hwp_max;
1140 if (update_hwp_desired)
1141 req.hwp_desired = req_update.hwp_desired;
1143 if (update_hwp_window)
1144 req.hwp_window = req_update.hwp_window;
1147 req.hwp_epp = req_update.hwp_epp;
1149 read_hwp_cap(cpu, &cap, MSR_HWP_CAPABILITIES);
1151 print_hwp_cap(cpu, &cap, "");
1154 check_hwp_request_v_hwp_capabilities(cpu, &req, &cap);
1156 verify_hwp_req_self_consistency(cpu, &req);
1158 write_hwp_request(cpu, &req, msr_offset);
1161 read_hwp_request(cpu, &req, msr_offset);
1162 print_hwp_request_pkg(pkg, &req, "new: ");
1167 int enable_hwp_on_cpu(int cpu)
1169 unsigned long long msr;
1171 get_msr(cpu, MSR_PM_ENABLE, &msr);
1172 put_msr(cpu, MSR_PM_ENABLE, 1);
1175 printf("cpu%d: MSR_PM_ENABLE old: %d new: %d\n", cpu, (unsigned int) msr, 1);
1180 int update_cpu_msrs(int cpu)
1182 unsigned long long msr;
1187 set_epb(cpu, new_epb);
1190 printf("cpu%d: ENERGY_PERF_BIAS old: %d new: %d\n",
1191 cpu, epb, (unsigned int) new_epb);
1195 int turbo_is_present_and_disabled;
1197 get_msr(cpu, MSR_IA32_MISC_ENABLE, &msr);
1199 turbo_is_present_and_disabled = ((msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE) != 0);
1201 if (turbo_update_value == 1) {
1202 if (turbo_is_present_and_disabled) {
1203 msr &= ~MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
1204 put_msr(cpu, MSR_IA32_MISC_ENABLE, msr);
1206 printf("cpu%d: turbo ENABLE\n", cpu);
1210 * if "turbo_is_enabled" were known to be describe this cpu
1211 * then we could use it here to skip redundant disable requests.
1212 * but cpu may be in a different package, so we always write.
1214 msr |= MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
1215 put_msr(cpu, MSR_IA32_MISC_ENABLE, msr);
1217 printf("cpu%d: turbo DISABLE\n", cpu);
1224 if (!hwp_update_enabled())
1227 update_hwp_request(cpu);
1231 unsigned int get_pkg_num(int cpu)
1238 sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1240 fp = fopen_or_die(pathname, "r");
1241 retval = fscanf(fp, "%d\n", &pkg);
1243 errx(1, "%s: failed to parse", pathname);
1247 int set_max_cpu_pkg_num(int cpu)
1251 if (max_cpu_num < cpu)
1254 pkg = get_pkg_num(cpu);
1256 if (pkg >= MAX_PACKAGES)
1257 errx(1, "cpu%d: %d >= MAX_PACKAGES (%d)", cpu, pkg, MAX_PACKAGES);
1259 if (pkg > max_pkg_num)
1262 if ((pkg_present_set & (1ULL << pkg)) == 0) {
1263 pkg_present_set |= (1ULL << pkg);
1264 first_cpu_in_pkg[pkg] = cpu;
1269 int mark_cpu_present(int cpu)
1271 CPU_SET_S(cpu, cpu_setsize, cpu_present_set);
1276 * run func(cpu) on every cpu in /proc/stat
1277 * return max_cpu number
1279 int for_all_proc_cpus(int (func)(int))
1285 fp = fopen_or_die(proc_stat, "r");
1287 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1289 err(1, "%s: failed to parse format", proc_stat);
1292 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1296 retval = func(cpu_num);
1306 void for_all_cpus_in_set(size_t set_size, cpu_set_t *cpu_set, int (func)(int))
1310 for (cpu_num = 0; cpu_num <= max_cpu_num; ++cpu_num)
1311 if (CPU_ISSET_S(cpu_num, set_size, cpu_set))
1315 void init_data_structures(void)
1317 for_all_proc_cpus(set_max_cpu_pkg_num);
1319 cpu_setsize = CPU_ALLOC_SIZE((max_cpu_num + 1));
1321 cpu_present_set = CPU_ALLOC((max_cpu_num + 1));
1322 if (cpu_present_set == NULL)
1323 err(3, "CPU_ALLOC");
1324 CPU_ZERO_S(cpu_setsize, cpu_present_set);
1325 for_all_proc_cpus(mark_cpu_present);
1328 /* clear has_hwp if it is not enable (or being enabled) */
1330 void verify_hwp_is_enabled(void)
1332 unsigned long long msr;
1334 if (!has_hwp) /* set in early_cpuid() */
1337 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
1338 get_msr(base_cpu, MSR_PM_ENABLE, &msr);
1339 if ((msr & 1) == 0) {
1340 fprintf(stderr, "HWP can be enabled using '--hwp-enable'\n");
1346 int req_update_bounds_check(void)
1348 if (!hwp_update_enabled())
1351 /* fail if min > max requested */
1352 if ((update_hwp_max && update_hwp_min) &&
1353 (req_update.hwp_min > req_update.hwp_max)) {
1354 printf("hwp-min %d > hwp_max %d\n", req_update.hwp_min, req_update.hwp_max);
1358 /* fail if desired > max requestd */
1359 if (req_update.hwp_desired && update_hwp_max &&
1360 (req_update.hwp_desired > req_update.hwp_max)) {
1361 printf("hwp-desired cannot be greater than hwp_max\n");
1364 /* fail if desired < min requestd */
1365 if (req_update.hwp_desired && update_hwp_min &&
1366 (req_update.hwp_desired < req_update.hwp_min)) {
1367 printf("hwp-desired cannot be less than hwp_min\n");
1374 void set_base_cpu(void)
1376 base_cpu = sched_getcpu();
1378 err(-ENODEV, "No valid cpus found");
1382 void probe_dev_msr(void)
1387 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1388 if (stat(pathname, &sb))
1389 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1390 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1393 static void get_cpuid_or_exit(unsigned int leaf,
1394 unsigned int *eax, unsigned int *ebx,
1395 unsigned int *ecx, unsigned int *edx)
1397 if (!__get_cpuid(leaf, eax, ebx, ecx, edx))
1398 errx(1, "Processor not supported\n");
1403 * initialize turbo_is_enabled, has_hwp, has_epb
1404 * before cmdline is parsed
1406 void early_cpuid(void)
1408 unsigned int eax, ebx, ecx, edx;
1409 unsigned int fms, family, model;
1411 get_cpuid_or_exit(1, &fms, &ebx, &ecx, &edx);
1412 family = (fms >> 8) & 0xf;
1413 model = (fms >> 4) & 0xf;
1414 if (family == 6 || family == 0xf)
1415 model += ((fms >> 16) & 0xf) << 4;
1417 if (model == 0x4F) {
1418 unsigned long long msr;
1420 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1422 bdx_highest_ratio = msr & 0xFF;
1425 get_cpuid_or_exit(0x6, &eax, &ebx, &ecx, &edx);
1426 turbo_is_enabled = (eax >> 1) & 1;
1427 has_hwp = (eax >> 7) & 1;
1428 has_epb = (ecx >> 3) & 1;
1434 * has_hwp, has_hwp_notify, has_hwp_activity_window, has_hwp_epp, has_hwp_request_pkg, has_epb
1436 void parse_cpuid(void)
1438 unsigned int eax, ebx, ecx, edx, max_level;
1439 unsigned int fms, family, model, stepping;
1441 eax = ebx = ecx = edx = 0;
1443 get_cpuid_or_exit(0, &max_level, &ebx, &ecx, &edx);
1445 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1449 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
1450 (char *)&ebx, (char *)&edx, (char *)&ecx);
1452 get_cpuid_or_exit(1, &fms, &ebx, &ecx, &edx);
1453 family = (fms >> 8) & 0xf;
1454 model = (fms >> 4) & 0xf;
1455 stepping = fms & 0xf;
1456 if (family == 6 || family == 0xf)
1457 model += ((fms >> 16) & 0xf) << 4;
1460 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1461 max_level, family, model, stepping, family, model, stepping);
1462 fprintf(stderr, "CPUID(1): %s %s %s %s %s %s %s %s\n",
1463 ecx & (1 << 0) ? "SSE3" : "-",
1464 ecx & (1 << 3) ? "MONITOR" : "-",
1465 ecx & (1 << 7) ? "EIST" : "-",
1466 ecx & (1 << 8) ? "TM2" : "-",
1467 edx & (1 << 4) ? "TSC" : "-",
1468 edx & (1 << 5) ? "MSR" : "-",
1469 edx & (1 << 22) ? "ACPI-TM" : "-",
1470 edx & (1 << 29) ? "TM" : "-");
1473 if (!(edx & (1 << 5)))
1474 errx(1, "CPUID: no MSR");
1477 get_cpuid_or_exit(0x6, &eax, &ebx, &ecx, &edx);
1478 /* turbo_is_enabled already set */
1479 /* has_hwp already set */
1480 has_hwp_notify = eax & (1 << 8);
1481 has_hwp_activity_window = eax & (1 << 9);
1482 has_hwp_epp = eax & (1 << 10);
1483 has_hwp_request_pkg = eax & (1 << 11);
1485 if (!has_hwp_request_pkg && update_hwp_use_pkg)
1486 errx(1, "--hwp-use-pkg is not available on this hardware");
1488 /* has_epb already set */
1492 "CPUID(6): %sTURBO, %sHWP, %sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
1493 turbo_is_enabled ? "" : "No-",
1494 has_hwp ? "" : "No-",
1495 has_hwp_notify ? "" : "No-",
1496 has_hwp_activity_window ? "" : "No-",
1497 has_hwp_epp ? "" : "No-",
1498 has_hwp_request_pkg ? "" : "No-",
1499 has_epb ? "" : "No-");
1501 return; /* success */
1504 int main(int argc, char **argv)
1508 init_data_structures();
1510 early_cpuid(); /* initial cpuid parse before cmdline */
1512 cmdline(argc, argv);
1519 /* If CPU-set and PKG-set are not initialized, default to all CPUs */
1520 if ((cpu_selected_set == 0) && (pkg_selected_set == 0))
1521 cpu_selected_set = cpu_present_set;
1524 * If HWP is being enabled, do it now, so that subsequent operations
1525 * that access HWP registers can work.
1527 if (update_hwp_enable)
1528 for_all_cpus_in_set(cpu_setsize, cpu_selected_set, enable_hwp_on_cpu);
1530 /* If HWP present, but disabled, warn and ignore from here forward */
1531 verify_hwp_is_enabled();
1533 if (req_update_bounds_check())
1536 /* display information only, no updates to settings */
1537 if (!update_epb && !update_turbo && !hwp_update_enabled()) {
1538 if (cpu_selected_set)
1539 for_all_cpus_in_set(cpu_setsize, cpu_selected_set, print_cpu_msrs);
1541 if (has_hwp_request_pkg) {
1542 if (pkg_selected_set == 0)
1543 pkg_selected_set = pkg_present_set;
1545 for_packages(pkg_selected_set, print_pkg_msrs);
1551 /* update CPU set */
1552 if (cpu_selected_set) {
1553 for_all_cpus_in_set(cpu_setsize, cpu_selected_set, update_sysfs);
1554 for_all_cpus_in_set(cpu_setsize, cpu_selected_set, update_cpu_msrs);
1555 } else if (pkg_selected_set)
1556 for_packages(pkg_selected_set, update_hwp_request_pkg);