1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Speed Select -- Enumerate and control features
4 * Copyright (c) 2019 Intel Corporation.
8 #include <linux/isst_if.h>
12 struct process_cmd_struct {
15 void (*process_fn)(int arg);
19 static const char *version_str = "v1.18";
21 static const int supported_api_ver = 2;
22 static struct isst_if_platform_info isst_platform_info;
23 static char *progname;
24 static int debug_flag;
28 static int cpu_stepping;
30 #define MAX_CPUS_IN_ONE_REQ 512
31 static short max_target_cpus;
32 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
34 static int topo_max_cpus;
35 static size_t present_cpumask_size;
36 static cpu_set_t *present_cpumask;
37 static size_t target_cpumask_size;
38 static cpu_set_t *target_cpumask;
39 static int tdp_level = 0xFF;
40 static int fact_bucket = 0xFF;
41 static int fact_avx = 0xFF;
42 static unsigned long long fact_trl;
43 static int out_format_json;
45 static int force_online_offline;
47 static int fact_enable_fail;
51 static int current_clos = -1;
52 static int clos_epp = -1;
53 static int clos_prop_prio = -1;
54 static int clos_min = -1;
55 static int clos_max = -1;
56 static int clos_desired = -1;
57 static int clos_priority_type;
58 static int cpu_0_cgroupv2;
59 static int cpu_0_workaround(int isolate);
62 unsigned short core_id;
63 unsigned short pkg_id;
64 unsigned short die_id;
65 unsigned short punit_id;
66 unsigned short punit_cpu;
67 unsigned short punit_cpu_core;
68 unsigned short initialized;
70 struct _cpu_map *cpu_map;
79 FILE *get_output_file(void)
84 int is_debug_enabled(void)
89 void debug_printf(const char *format, ...)
93 va_start(args, format);
96 vprintf(format, args);
102 int is_clx_n_platform(void)
104 if (cpu_model == 0x55)
105 if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
110 int is_skx_based_platform(void)
112 if (cpu_model == 0x55)
118 int is_spr_platform(void)
120 if (cpu_model == 0x8F)
126 int is_emr_platform(void)
128 if (cpu_model == 0xCF)
135 int is_icx_platform(void)
137 if (cpu_model == 0x6A || cpu_model == 0x6C)
143 static int update_cpu_model(void)
145 unsigned int ebx, ecx, edx;
146 unsigned int fms, family;
148 __cpuid(1, fms, ebx, ecx, edx);
149 family = (fms >> 8) & 0xf;
150 cpu_model = (fms >> 4) & 0xf;
151 if (family == 6 || family == 0xf)
152 cpu_model += ((fms >> 16) & 0xf) << 4;
154 cpu_stepping = fms & 0xf;
155 /* only three CascadeLake-N models are supported */
156 if (is_clx_n_platform()) {
162 fp = fopen("/proc/cpuinfo", "r");
164 err(-1, "cannot open /proc/cpuinfo\n");
166 while (getline(&line, &n, fp) > 0) {
167 if (strstr(line, "model name")) {
168 if (strstr(line, "6252N") ||
169 strstr(line, "6230N") ||
170 strstr(line, "5218N"))
182 int api_version(void)
184 return isst_platform_info.api_version;
187 /* Open a file, and exit on failure */
188 static FILE *fopen_or_exit(const char *path, const char *mode)
190 FILE *filep = fopen(path, mode);
193 err(1, "%s: open failed", path);
198 /* Parse a file containing a single int */
199 static int parse_int_file(int fatal, const char *fmt, ...)
207 vsnprintf(path, sizeof(path), fmt, args);
210 filep = fopen_or_exit(path, "r");
212 filep = fopen(path, "r");
216 if (fscanf(filep, "%d", &value) != 1)
217 err(1, "%s: failed to parse number from file", path);
223 int cpufreq_sysfs_present(void)
227 dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
236 int out_format_is_json(void)
238 return out_format_json;
241 static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
243 const char *pathname = "/var/run/isst_cpu_topology.dat";
244 struct cpu_topology cpu_top;
248 fp = fopen(pathname, "rb");
252 ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
256 ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
262 *pkg_id = cpu_top.pkg_id;
263 *core_id = cpu_top.core_id;
264 *die_id = cpu_top.die_id;
273 static void store_cpu_topology(void)
275 const char *pathname = "/var/run/isst_cpu_topology.dat";
279 fp = fopen(pathname, "rb");
281 /* Mapping already exists */
286 fp = fopen(pathname, "wb");
288 fprintf(stderr, "Can't create file:%s\n", pathname);
292 fprintf(stderr, "Caching topology information\n");
294 for (i = 0; i < topo_max_cpus; ++i) {
295 struct cpu_topology cpu_top;
297 cpu_top.core_id = parse_int_file(0,
298 "/sys/devices/system/cpu/cpu%d/topology/core_id", i);
299 if (cpu_top.core_id < 0)
300 cpu_top.core_id = -1;
302 cpu_top.pkg_id = parse_int_file(0,
303 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
304 if (cpu_top.pkg_id < 0)
307 cpu_top.die_id = parse_int_file(0,
308 "/sys/devices/system/cpu/cpu%d/topology/die_id", i);
309 if (cpu_top.die_id < 0)
314 if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
315 fprintf(stderr, "Can't write to:%s\n", pathname);
323 static int get_physical_package_id(int cpu)
330 if (cpu_map && cpu_map[cpu].initialized)
331 return cpu_map[cpu].pkg_id;
333 ret = parse_int_file(0,
334 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
337 int core_id, pkg_id, die_id;
339 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
347 static int get_physical_core_id(int cpu)
354 if (cpu_map && cpu_map[cpu].initialized)
355 return cpu_map[cpu].core_id;
357 ret = parse_int_file(0,
358 "/sys/devices/system/cpu/cpu%d/topology/core_id",
361 int core_id, pkg_id, die_id;
363 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
371 static int get_physical_die_id(int cpu)
378 if (cpu_map && cpu_map[cpu].initialized)
379 return cpu_map[cpu].die_id;
381 ret = parse_int_file(0,
382 "/sys/devices/system/cpu/cpu%d/topology/die_id",
385 int core_id, pkg_id, die_id;
387 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
402 static int get_physical_punit_id(int cpu)
407 if (cpu_map && cpu_map[cpu].initialized)
408 return cpu_map[cpu].punit_id;
413 void set_isst_id(struct isst_id *id, int cpu)
417 id->pkg = get_physical_package_id(cpu);
418 if (id->pkg >= MAX_PACKAGE_COUNT)
421 id->die = get_physical_die_id(cpu);
422 if (id->die >= MAX_DIE_PER_PACKAGE)
425 id->punit = get_physical_punit_id(cpu);
426 if (id->punit >= MAX_PUNIT_PER_DIE)
430 int is_cpu_in_power_domain(int cpu, struct isst_id *id)
434 set_isst_id(&tid, cpu);
436 if (id->pkg == tid.pkg && id->die == tid.die && id->punit == tid.punit)
442 int get_cpufreq_base_freq(int cpu)
444 return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
447 int get_topo_max_cpus(void)
449 return topo_max_cpus;
452 static unsigned int is_cpu_online(int cpu)
456 unsigned char online;
458 snprintf(buffer, sizeof(buffer),
459 "/sys/devices/system/cpu/cpu%d/online", cpu);
461 fd = open(buffer, O_RDONLY);
465 ret = read(fd, &online, sizeof(online));
479 void set_cpu_online_offline(int cpu, int state)
484 if (cpu_0_cgroupv2 && !cpu) {
485 fprintf(stderr, "Will use cgroup v2 for CPU 0\n");
486 cpu_0_workaround(!state);
490 snprintf(buffer, sizeof(buffer),
491 "/sys/devices/system/cpu/cpu%d/online", cpu);
493 fd = open(buffer, O_WRONLY);
496 fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
497 fprintf(stderr, "Will use cgroup v2\n");
498 cpu_0_workaround(!state);
501 err(-1, "%s open failed", buffer);
505 ret = write(fd, "1\n", 2);
507 ret = write(fd, "0\n", 2);
510 perror("Online/Offline: Operation failed\n");
515 static void force_all_cpus_online(void)
519 fprintf(stderr, "Forcing all CPUs online\n");
521 for (i = 0; i < topo_max_cpus; ++i)
522 set_cpu_online_offline(i, 1);
524 unlink("/var/run/isst_cpu_topology.dat");
527 void for_each_online_power_domain_in_set(void (*callback)(struct isst_id *, void *, void *,
529 void *arg1, void *arg2, void *arg3,
533 int cpus[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
534 int valid_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE] = {0};
537 memset(cpus, -1, sizeof(cpus));
539 for (i = 0; i < topo_max_cpus; ++i) {
542 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
545 online = parse_int_file(
546 i != 0, "/sys/devices/system/cpu/cpu%d/online", i);
548 online = 1; /* online entry for CPU 0 needs some special configs */
555 if (id.pkg < 0 || id.die < 0 || id.punit < 0)
558 valid_mask[id.pkg][id.die] = 1;
560 if (cpus[id.pkg][id.die][id.punit] == -1)
561 cpus[id.pkg][id.die][id.punit] = i;
564 for (i = 0; i < MAX_PACKAGE_COUNT; i++) {
565 for (j = 0; j < MAX_DIE_PER_PACKAGE; j++) {
568 * How to check a non-cpu die for a package/die with all cpu offlined?
570 if (!valid_mask[i][j])
572 for (k = 0; k < MAX_PUNIT_PER_DIE; k++) {
573 id.cpu = cpus[i][j][k];
577 if (isst_is_punit_valid(&id))
578 callback(&id, arg1, arg2, arg3, arg4);
584 static void for_each_online_target_cpu_in_set(
585 void (*callback)(struct isst_id *, void *, void *, void *, void *), void *arg1,
586 void *arg2, void *arg3, void *arg4)
591 for (i = 0; i < topo_max_cpus; ++i) {
594 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
597 online = parse_int_file(
598 1, "/sys/devices/system/cpu/cpu%d/online", i);
601 1; /* online entry for CPU 0 needs some special configs */
604 if (online && callback) {
605 callback(&id, arg1, arg2, arg3, arg4);
611 fprintf(stderr, "No valid CPU in the list\n");
614 #define BITMASK_SIZE 32
615 static void set_max_cpu_num(void)
622 for (i = 0; i < 256; ++i) {
625 snprintf(path, sizeof(path),
626 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
627 filep = fopen(path, "r");
633 fprintf(stderr, "Can't get max cpu number\n");
637 while (fscanf(filep, "%lx,", &dummy) == 1)
638 topo_max_cpus += BITMASK_SIZE;
641 debug_printf("max cpus %d\n", topo_max_cpus);
644 size_t alloc_cpu_set(cpu_set_t **cpu_set)
649 _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
650 if (_cpu_set == NULL)
652 size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
653 CPU_ZERO_S(size, _cpu_set);
659 void free_cpu_set(cpu_set_t *cpu_set)
664 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
666 int get_max_punit_core_id(struct isst_id *id)
671 for (i = 0; i < topo_max_cpus; ++i)
673 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
676 if (is_cpu_in_power_domain(i, id) &&
677 cpu_map[i].punit_cpu_core > max_id)
678 max_id = cpu_map[i].punit_cpu_core;
684 int get_cpu_count(struct isst_id *id)
686 if (id->pkg < 0 || id->die < 0 || id->punit < 0)
689 return cpu_cnt[id->pkg][id->die][id->punit];
692 static void update_punit_cpu_info(__u32 physical_cpu, struct _cpu_map *cpu_map)
694 if (api_version() > 1) {
697 * [15:11] PM_DOMAIN_ID
698 * [10:3] MODULE_ID (aka IDI_AGENT_ID)
699 * [2:0] LP_ID (We don't care about these bits we only
700 * care die and core id
703 * [1:0] core ID within module
708 cpu_map->punit_id = (physical_cpu >> 11) & 0x1f;
709 cpu_map->punit_cpu_core = (physical_cpu >> 3) & 0xff;
710 cpu_map->punit_cpu = physical_cpu & 0x7ff;
717 * Bit 0 – thread ID
718 * Bit 8:1 – core ID
719 * Bit 13:9 – punit ID
721 cpu_map->punit_cpu = physical_cpu & 0x1ff;
722 cpu_map->punit_cpu_core = (cpu_map->punit_cpu >> 1); // shift to get core id
723 punit_id = (physical_cpu >> 9) & 0x1f;
725 if (punit_id >= MAX_PUNIT_PER_DIE)
728 cpu_map->punit_id = punit_id;
732 static void create_cpu_map(void)
734 const char *pathname = "/dev/isst_interface";
738 struct isst_if_cpu_maps map;
740 /* Use calloc to make sure the memory is initialized to Zero */
741 cpu_map = calloc(topo_max_cpus, sizeof(*cpu_map));
745 fd = open(pathname, O_RDWR);
746 if (fd < 0 && !is_clx_n_platform())
747 err(-1, "%s open failed", pathname);
749 size = alloc_cpu_set(&present_cpumask);
750 present_cpumask_size = size;
752 for (i = 0; i < topo_max_cpus; ++i) {
754 int pkg_id, die_id, core_id, punit_id;
756 /* check if CPU is online */
757 snprintf(buffer, sizeof(buffer),
758 "/sys/devices/system/cpu/cpu%d", i);
759 dir = opendir(buffer);
764 CPU_SET_S(i, size, present_cpumask);
766 pkg_id = get_physical_package_id(i);
767 die_id = get_physical_die_id(i);
768 core_id = get_physical_core_id(i);
770 if (pkg_id < 0 || die_id < 0 || core_id < 0)
773 cpu_map[i].pkg_id = pkg_id;
774 cpu_map[i].die_id = die_id;
775 cpu_map[i].core_id = core_id;
782 map.cpu_map[0].logical_cpu = i;
783 debug_printf(" map logical_cpu:%d\n",
784 map.cpu_map[0].logical_cpu);
785 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
786 perror("ISST_IF_GET_PHY_ID");
787 fprintf(outf, "Error: map logical_cpu:%d\n",
788 map.cpu_map[0].logical_cpu);
790 update_punit_cpu_info(map.cpu_map[0].physical_cpu, &cpu_map[i]);
791 punit_id = cpu_map[i].punit_id;
794 cpu_map[i].initialized = 1;
796 cpu_cnt[pkg_id][die_id][punit_id]++;
799 "map logical_cpu:%d core: %d die:%d pkg:%d punit:%d punit_cpu:%d punit_core:%d\n",
800 i, cpu_map[i].core_id, cpu_map[i].die_id,
801 cpu_map[i].pkg_id, cpu_map[i].punit_id,
802 cpu_map[i].punit_cpu, cpu_map[i].punit_cpu_core);
807 size = alloc_cpu_set(&target_cpumask);
808 target_cpumask_size = size;
809 for (i = 0; i < max_target_cpus; ++i) {
810 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
814 CPU_SET_S(target_cpus[i], size, target_cpumask);
818 void set_cpu_mask_from_punit_coremask(struct isst_id *id, unsigned long long core_mask,
819 size_t core_cpumask_size,
820 cpu_set_t *core_cpumask, int *cpu_cnt)
829 for (i = 0; i < 64; ++i) {
830 if (core_mask & BIT_ULL(i)) {
833 for (j = 0; j < topo_max_cpus; ++j) {
834 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
837 if (is_cpu_in_power_domain(j, id) &&
838 cpu_map[j].punit_cpu_core == i) {
839 CPU_SET_S(j, core_cpumask_size,
850 int find_phy_core_num(int logical_cpu)
852 if (logical_cpu < topo_max_cpus)
853 return cpu_map[logical_cpu].punit_cpu_core;
858 int use_cgroupv2(void)
863 int enable_cpuset_controller(void)
867 fd = open("/sys/fs/cgroup/cgroup.subtree_control", O_RDWR, 0);
869 debug_printf("Can't activate cpuset controller\n");
870 debug_printf("Either you are not root user or CGroup v2 is not supported\n");
874 ret = write(fd, " +cpuset", strlen(" +cpuset"));
878 debug_printf("Can't activate cpuset controller: Write failed\n");
885 int isolate_cpus(struct isst_id *id, int mask_size, cpu_set_t *cpu_mask, int level, int cpu_0_only)
887 int i, first, curr_index, index, ret, fd;
888 static char str[512], dir_name[64];
889 static char cpuset_cpus[128];
890 int str_len = sizeof(str);
893 snprintf(dir_name, sizeof(dir_name), "/sys/fs/cgroup/%d-%d-%d", id->pkg, id->die, id->punit);
894 dir = opendir(dir_name);
896 ret = mkdir(dir_name, 0744);
898 debug_printf("Can't create dir:%s errno:%d\n", dir_name, errno);
905 sprintf(cpuset_cpus, "%s/cpuset.cpus.partition", dir_name);
907 fd = open(cpuset_cpus, O_RDWR, 0);
912 ret = write(fd, "member", strlen("member"));
914 printf("Can't update to member\n");
921 if (!CPU_COUNT_S(mask_size, cpu_mask)) {
930 snprintf(str, str_len, "0");
931 goto create_partition;
934 for (i = 0; i < get_topo_max_cpus(); ++i) {
935 if (!is_cpu_in_power_domain(i, id))
938 if (CPU_ISSET_S(i, mask_size, cpu_mask))
942 index = snprintf(&str[curr_index],
943 str_len - curr_index, ",");
945 if (curr_index >= str_len)
948 index = snprintf(&str[curr_index], str_len - curr_index, "%d",
951 if (curr_index >= str_len)
957 debug_printf("isolated CPUs list: package:%d curr_index:%d [%s]\n", id->pkg, curr_index ,str);
959 snprintf(cpuset_cpus, sizeof(cpuset_cpus), "%s/cpuset.cpus", dir_name);
961 fd = open(cpuset_cpus, O_RDWR, 0);
966 ret = write(fd, str, strlen(str));
970 debug_printf("Can't activate cpuset controller: Write failed\n");
974 snprintf(cpuset_cpus, sizeof(cpuset_cpus), "%s/cpuset.cpus.partition", dir_name);
976 fd = open(cpuset_cpus, O_RDWR, 0);
981 ret = write(fd, "isolated", strlen("isolated"));
983 debug_printf("Can't update to isolated\n");
984 ret = write(fd, "root", strlen("root"));
986 debug_printf("Can't update to root\n");
997 static int cpu_0_workaround(int isolate)
999 int fd, fd1, len, ret;
1004 debug_printf("isolate CPU 0 state: %d\n", isolate);
1009 /* First check if CPU 0 was isolated to remove isolation. */
1011 /* If the cpuset.cpus doesn't exist, that means that none of the CPUs are isolated*/
1012 fd = open("/sys/fs/cgroup/0-0-0/cpuset.cpus", O_RDONLY, 0);
1016 len = read(fd, str, sizeof(str));
1017 /* Error check, but unlikely to fail. If fails that means that not isolated */
1022 /* Is CPU 0 is in isolate list, the display is sorted so first element will be CPU 0*/
1023 if (str[0] != '0') {
1028 fd1 = open("/sys/fs/cgroup/0-0-0/cpuset.cpus.partition", O_RDONLY, 0);
1029 /* Unlikely that, this attribute is not present, but handle error */
1035 /* Is CPU 0 already changed partition to "member" */
1036 len = read(fd1, str, sizeof(str));
1037 if (len != -1 && str[0] == 'm') {
1046 debug_printf("CPU 0 was isolated before, so remove isolation\n");
1049 ret = enable_cpuset_controller();
1053 CPU_ZERO(&cpu_mask);
1054 memset(&id, 0, sizeof(struct isst_id));
1055 CPU_SET(0, &cpu_mask);
1057 ret = isolate_cpus(&id, sizeof(cpu_mask), &cpu_mask, isolate, 1);
1060 fprintf(stderr, "Can't isolate CPU 0\n");
1065 static int isst_fill_platform_info(void)
1067 const char *pathname = "/dev/isst_interface";
1070 if (is_clx_n_platform()) {
1071 isst_platform_info.api_version = 1;
1072 goto set_platform_ops;
1075 fd = open(pathname, O_RDWR);
1077 err(-1, "%s open failed", pathname);
1079 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
1080 perror("ISST_IF_GET_PLATFORM_INFO");
1087 if (isst_platform_info.api_version > supported_api_ver) {
1088 printf("Incompatible API versions; Upgrade of tool is required\n");
1093 if (isst_set_platform_ops(isst_platform_info.api_version)) {
1094 fprintf(stderr, "Failed to set platform callbacks\n");
1100 void get_isst_status(struct isst_id *id, void *arg1, void *arg2, void *arg3, void *arg4)
1102 struct isst_pkg_ctdp pkg_dev;
1103 struct isst_id *tid = (struct isst_id *)arg2;
1104 int *mask = (int *)arg3;
1105 int *max_level = (int *)arg4;
1108 /* Only check the first cpu power domain */
1109 if (id->cpu < 0 || tid->cpu >= 0)
1112 ret = isst_get_ctdp_levels(id, &pkg_dev);
1116 if (pkg_dev.enabled)
1122 if (*max_level < pkg_dev.levels)
1123 *max_level = pkg_dev.levels;
1125 for (j = 0; j <= pkg_dev.levels; ++j) {
1126 struct isst_pkg_ctdp_level_info ctdp_level;
1128 ret = isst_get_ctdp_control(id, j, &ctdp_level);
1132 if (ctdp_level.fact_support)
1135 if (ctdp_level.pbf_support)
1142 tid->punit = id->punit;
1145 static void isst_print_extended_platform_info(void)
1147 int cp_state, cp_cap;
1149 int mask = 0, max_level = 0;
1152 for_each_online_power_domain_in_set(get_isst_status, NULL, &id, &mask, &max_level);
1154 if (mask & BIT(0)) {
1155 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
1157 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
1158 fprintf(outf, "Only performance level 0 (base level) is present\n");
1162 fprintf(outf, "TDP level change control is locked\n");
1164 fprintf(outf, "TDP level change control is unlocked, max level: %d\n", max_level);
1167 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
1169 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
1172 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
1174 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
1176 if (isst_read_pm_config(&id, &cp_state, &cp_cap)) {
1177 fprintf(outf, "Intel(R) SST-CP (feature core-power) status is unknown\n");
1182 fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
1184 fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
1187 static void isst_print_platform_information(void)
1189 if (is_clx_n_platform()) {
1190 fprintf(stderr, "\nThis option in not supported on this platform\n");
1194 /* Early initialization to create working cpu_map */
1198 fprintf(outf, "Platform: API version : %d\n",
1199 isst_platform_info.api_version);
1200 fprintf(outf, "Platform: Driver version : %d\n",
1201 isst_platform_info.driver_version);
1202 fprintf(outf, "Platform: mbox supported : %d\n",
1203 isst_platform_info.mbox_supported);
1204 fprintf(outf, "Platform: mmio supported : %d\n",
1205 isst_platform_info.mmio_supported);
1206 isst_print_extended_platform_info();
1211 static char *local_str0, *local_str1;
1212 static void exec_on_get_ctdp_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1215 int (*fn_ptr)(struct isst_id *id, void *arg);
1219 ret = fn_ptr(id, arg2);
1221 isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
1223 isst_ctdp_display_core_info(id, outf, arg3,
1224 *(unsigned int *)arg4,
1225 local_str0, local_str1);
1228 #define _get_tdp_level(desc, suffix, object, help, str0, str1) \
1229 static void get_tdp_##object(int arg) \
1231 struct isst_pkg_ctdp ctdp; \
1235 "Print %s [No command arguments are required]\n", \
1239 local_str0 = str0; \
1240 local_str1 = str1; \
1241 isst_ctdp_display_information_start(outf); \
1242 if (max_target_cpus) \
1243 for_each_online_target_cpu_in_set( \
1244 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix, \
1245 &ctdp, desc, &ctdp.object); \
1247 for_each_online_power_domain_in_set(exec_on_get_ctdp_cpu, \
1248 isst_get_ctdp_##suffix, \
1251 isst_ctdp_display_information_end(outf); \
1254 _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
1255 _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
1256 _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
1257 _get_tdp_level("get-config-current_level", levels, current_level,
1258 "Current TDP Level", NULL, NULL);
1259 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
1261 struct isst_pkg_ctdp clx_n_pkg_dev;
1263 static int clx_n_get_base_ratio(void)
1266 char *begin, *end, *line = NULL;
1271 fp = fopen("/proc/cpuinfo", "r");
1273 err(-1, "cannot open /proc/cpuinfo\n");
1275 while (getline(&line, &n, fp) > 0) {
1276 if (strstr(line, "model name")) {
1277 /* this is true for CascadeLake-N */
1278 begin = strstr(line, "@ ") + 2;
1279 end = strstr(line, "GHz");
1280 strncpy(number, begin, end - begin);
1281 value = atof(number) * 10;
1288 return (int)(value);
1291 static int clx_n_config(struct isst_id *id)
1294 unsigned long cpu_bf;
1295 struct isst_pkg_ctdp_level_info *ctdp_level;
1296 struct isst_pbf_info *pbf_info;
1298 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1299 pbf_info = &ctdp_level->pbf_info;
1300 ctdp_level->core_cpumask_size =
1301 alloc_cpu_set(&ctdp_level->core_cpumask);
1303 /* find the frequency base ratio */
1304 ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1305 if (ctdp_level->tdp_ratio == 0) {
1306 debug_printf("CLX: cn base ratio is zero\n");
1311 /* find the high and low priority frequencies */
1312 pbf_info->p1_high = 0;
1313 pbf_info->p1_low = ~0;
1315 for (i = 0; i < topo_max_cpus; i++) {
1316 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1319 if (!is_cpu_in_power_domain(i, id))
1322 CPU_SET_S(i, ctdp_level->core_cpumask_size,
1323 ctdp_level->core_cpumask);
1325 cpu_bf = parse_int_file(1,
1326 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1328 if (cpu_bf > pbf_info->p1_high)
1329 pbf_info->p1_high = cpu_bf;
1330 if (cpu_bf < pbf_info->p1_low)
1331 pbf_info->p1_low = cpu_bf;
1334 if (pbf_info->p1_high == ~0UL) {
1335 debug_printf("CLX: maximum base frequency not set\n");
1340 if (pbf_info->p1_low == 0) {
1341 debug_printf("CLX: minimum base frequency not set\n");
1346 /* convert frequencies back to ratios */
1347 pbf_info->p1_high = pbf_info->p1_high / 100000;
1348 pbf_info->p1_low = pbf_info->p1_low / 100000;
1350 /* create high priority cpu mask */
1351 pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1352 for (i = 0; i < topo_max_cpus; i++) {
1353 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1356 if (!is_cpu_in_power_domain(i, id))
1359 cpu_bf = parse_int_file(1,
1360 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1362 cpu_bf = cpu_bf / 100000;
1363 if (cpu_bf == pbf_info->p1_high)
1364 CPU_SET_S(i, pbf_info->core_cpumask_size,
1365 pbf_info->core_cpumask);
1368 /* extra ctdp & pbf struct parameters */
1369 ctdp_level->processed = 1;
1370 ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1371 ctdp_level->pbf_enabled = 1;
1372 ctdp_level->fact_support = 0; /* FACT is never supported */
1373 ctdp_level->fact_enabled = 0;
1378 free_cpu_set(ctdp_level->core_cpumask);
1382 static void dump_clx_n_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1383 void *arg3, void *arg4)
1387 if (tdp_level != 0xff && tdp_level != 0) {
1388 isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1392 ret = clx_n_config(id);
1394 debug_printf("clx_n_config failed");
1396 struct isst_pkg_ctdp_level_info *ctdp_level;
1397 struct isst_pbf_info *pbf_info;
1399 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1400 pbf_info = &ctdp_level->pbf_info;
1401 clx_n_pkg_dev.processed = 1;
1402 isst_ctdp_display_information(id, outf, tdp_level, &clx_n_pkg_dev);
1403 free_cpu_set(ctdp_level->core_cpumask);
1404 free_cpu_set(pbf_info->core_cpumask);
1408 static void dump_isst_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1409 void *arg3, void *arg4)
1411 struct isst_pkg_ctdp pkg_dev;
1414 memset(&pkg_dev, 0, sizeof(pkg_dev));
1415 ret = isst_get_process_ctdp(id, tdp_level, &pkg_dev);
1417 isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, id->cpu);
1418 isst_ctdp_display_information_end(outf);
1421 isst_ctdp_display_information(id, outf, tdp_level, &pkg_dev);
1422 isst_get_process_ctdp_complete(id, &pkg_dev);
1426 static void dump_isst_config(int arg)
1432 "Print Intel(R) Speed Select Technology Performance profile configuration\n");
1434 "including base frequency and turbo frequency configurations\n");
1435 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
1437 "\tIf no arguments, dump information for all TDP levels\n");
1441 if (!is_clx_n_platform())
1442 fn = dump_isst_config_for_cpu;
1444 fn = dump_clx_n_config_for_cpu;
1446 isst_ctdp_display_information_start(outf);
1448 if (max_target_cpus)
1449 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1451 for_each_online_power_domain_in_set(fn, NULL, NULL, NULL, NULL);
1453 isst_ctdp_display_information_end(outf);
1456 static void adjust_scaling_max_from_base_freq(int cpu);
1458 static void set_tdp_level_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1461 struct isst_pkg_ctdp pkg_dev;
1464 ret = isst_get_ctdp_levels(id, &pkg_dev);
1466 isst_display_error_info_message(1, "Get TDP level failed", 0, 0);
1467 isst_ctdp_display_information_end(outf);
1471 if (pkg_dev.current_level == tdp_level) {
1472 debug_printf("TDP level already set. Skipped\n");
1473 goto display_result;
1476 ret = isst_set_tdp_level(id, tdp_level);
1478 isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1479 isst_ctdp_display_information_end(outf);
1484 isst_display_result(id, outf, "perf-profile", "set_tdp_level", ret);
1485 if (force_online_offline && id->cpu >= 0) {
1486 struct isst_pkg_ctdp_level_info ctdp_level;
1488 /* Wait for updated base frequencies */
1491 /* Adjusting uncore freq */
1492 isst_adjust_uncore_freq(id, tdp_level, &ctdp_level);
1494 fprintf(stderr, "Option is set to online/offline\n");
1495 ctdp_level.core_cpumask_size =
1496 alloc_cpu_set(&ctdp_level.core_cpumask);
1497 ret = isst_get_coremask_info(id, tdp_level, &ctdp_level);
1499 isst_display_error_info_message(1, "Can't get coremask, online/offline option is ignored", 0, 0);
1503 if (use_cgroupv2()) {
1506 fprintf(stderr, "Using cgroup v2 in lieu of online/offline\n");
1507 ret = enable_cpuset_controller();
1511 ret = isolate_cpus(id, ctdp_level.core_cpumask_size,
1512 ctdp_level.core_cpumask, tdp_level, 0);
1520 if (ctdp_level.cpu_count) {
1521 int i, max_cpus = get_topo_max_cpus();
1522 for (i = 0; i < max_cpus; ++i) {
1523 if (!is_cpu_in_power_domain(i, id))
1525 if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
1526 fprintf(stderr, "online cpu %d\n", i);
1527 set_cpu_online_offline(i, 1);
1528 adjust_scaling_max_from_base_freq(i);
1530 fprintf(stderr, "offline cpu %d\n", i);
1531 set_cpu_online_offline(i, 0);
1536 free_cpu_set(ctdp_level.core_cpumask);
1540 static void set_tdp_level(int arg)
1543 fprintf(stderr, "Set Config TDP level\n");
1545 "\t Arguments: -l|--level : Specify tdp level\n");
1547 "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1549 "\t online/offline operation has limitations, refer to Linux hotplug documentation\n");
1553 if (tdp_level == 0xff) {
1554 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1557 isst_ctdp_display_information_start(outf);
1558 if (max_target_cpus)
1559 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
1562 for_each_online_power_domain_in_set(set_tdp_level_for_cpu, NULL,
1564 isst_ctdp_display_information_end(outf);
1567 static void clx_n_dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1568 void *arg3, void *arg4)
1572 ret = clx_n_config(id);
1574 isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
1576 struct isst_pkg_ctdp_level_info *ctdp_level;
1577 struct isst_pbf_info *pbf_info;
1579 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1580 pbf_info = &ctdp_level->pbf_info;
1581 isst_pbf_display_information(id, outf, tdp_level, pbf_info);
1582 free_cpu_set(ctdp_level->core_cpumask);
1583 free_cpu_set(pbf_info->core_cpumask);
1587 static void dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1590 struct isst_pbf_info pbf_info;
1593 ret = isst_get_pbf_info(id, tdp_level, &pbf_info);
1595 isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
1596 isst_ctdp_display_information_end(outf);
1599 isst_pbf_display_information(id, outf, tdp_level, &pbf_info);
1600 free_cpu_set(pbf_info.core_cpumask);
1604 static void dump_pbf_config(int arg)
1610 "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
1612 "\tArguments: -l|--level : Specify tdp level\n");
1616 if (tdp_level == 0xff) {
1617 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1621 if (!is_clx_n_platform())
1622 fn = dump_pbf_config_for_cpu;
1624 fn = clx_n_dump_pbf_config_for_cpu;
1626 isst_ctdp_display_information_start(outf);
1628 if (max_target_cpus)
1629 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1631 for_each_online_power_domain_in_set(fn, NULL, NULL, NULL, NULL);
1633 isst_ctdp_display_information_end(outf);
1636 static int set_clos_param(struct isst_id *id, int clos, int epp, int wt, int min, int max)
1638 struct isst_clos_config clos_config;
1641 ret = isst_pm_get_clos(id, clos, &clos_config);
1643 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1646 clos_config.clos_min = min;
1647 clos_config.clos_max = max;
1648 clos_config.epp = epp;
1649 clos_config.clos_prop_prio = wt;
1650 ret = isst_set_clos(id, clos, &clos_config);
1652 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1659 static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1661 char buffer[128], freq_str[16];
1665 snprintf(buffer, sizeof(buffer),
1666 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1668 snprintf(buffer, sizeof(buffer),
1669 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1671 fd = open(buffer, O_WRONLY);
1675 snprintf(freq_str, sizeof(freq_str), "%d", freq);
1676 len = strlen(freq_str);
1677 ret = write(fd, freq_str, len);
1687 static int no_turbo(void)
1689 return parse_int_file(0, "/sys/devices/system/cpu/intel_pstate/no_turbo");
1692 static void adjust_scaling_max_from_base_freq(int cpu)
1694 int base_freq, scaling_max_freq;
1696 scaling_max_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1697 base_freq = get_cpufreq_base_freq(cpu);
1698 if (scaling_max_freq < base_freq || no_turbo())
1699 set_cpufreq_scaling_min_max(cpu, 1, base_freq);
1702 static void adjust_scaling_min_from_base_freq(int cpu)
1704 int base_freq, scaling_min_freq;
1706 scaling_min_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1707 base_freq = get_cpufreq_base_freq(cpu);
1708 if (scaling_min_freq < base_freq)
1709 set_cpufreq_scaling_min_max(cpu, 0, base_freq);
1712 static int set_clx_pbf_cpufreq_scaling_min_max(struct isst_id *id)
1714 struct isst_pkg_ctdp_level_info *ctdp_level;
1715 struct isst_pbf_info *pbf_info;
1716 int i, freq, freq_high, freq_low;
1719 ret = clx_n_config(id);
1721 debug_printf("cpufreq_scaling_min_max failed for CLX");
1725 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1726 pbf_info = &ctdp_level->pbf_info;
1727 freq_high = pbf_info->p1_high * 100000;
1728 freq_low = pbf_info->p1_low * 100000;
1730 for (i = 0; i < get_topo_max_cpus(); ++i) {
1731 if (!is_cpu_in_power_domain(i, id))
1734 if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1735 pbf_info->core_cpumask))
1740 set_cpufreq_scaling_min_max(i, 1, freq);
1741 set_cpufreq_scaling_min_max(i, 0, freq);
1747 static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1749 char buffer[128], min_freq[16];
1752 if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1756 snprintf(buffer, sizeof(buffer),
1757 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1759 snprintf(buffer, sizeof(buffer),
1760 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1762 fd = open(buffer, O_RDONLY);
1766 len = read(fd, min_freq, sizeof(min_freq));
1773 snprintf(buffer, sizeof(buffer),
1774 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1776 snprintf(buffer, sizeof(buffer),
1777 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1779 fd = open(buffer, O_WRONLY);
1783 min_freq[15] = '\0';
1784 len = strlen(min_freq);
1785 ret = write(fd, min_freq, len);
1795 static void set_scaling_min_to_cpuinfo_max(struct isst_id *id)
1802 for (i = 0; i < get_topo_max_cpus(); ++i) {
1803 if (!is_cpu_in_power_domain(i, id))
1806 if (is_cpu_online(i) != 1)
1809 adjust_scaling_max_from_base_freq(i);
1810 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1811 adjust_scaling_min_from_base_freq(i);
1815 static void set_scaling_min_to_cpuinfo_min(struct isst_id *id)
1822 for (i = 0; i < get_topo_max_cpus(); ++i) {
1823 if (!is_cpu_in_power_domain(i, id))
1826 if (is_cpu_online(i) != 1)
1829 adjust_scaling_max_from_base_freq(i);
1830 set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1834 static void set_scaling_max_to_cpuinfo_max(struct isst_id *id)
1838 for (i = 0; i < get_topo_max_cpus(); ++i) {
1839 if (!is_cpu_in_power_domain(i, id))
1842 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1846 static int set_core_priority_and_min(struct isst_id *id, int mask_size,
1847 cpu_set_t *cpu_mask, int min_high,
1852 if (!CPU_COUNT_S(mask_size, cpu_mask))
1855 ret = set_clos_param(id, 0, 0, 0, min_high, 0xff);
1859 ret = set_clos_param(id, 1, 15, 15, min_low, 0xff);
1863 ret = set_clos_param(id, 2, 15, 15, min_low, 0xff);
1867 ret = set_clos_param(id, 3, 15, 15, min_low, 0xff);
1871 for (i = 0; i < get_topo_max_cpus(); ++i) {
1875 if (!is_cpu_in_power_domain(i, id))
1878 if (CPU_ISSET_S(i, mask_size, cpu_mask))
1883 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1884 set_isst_id(&tid, i);
1885 ret = isst_clos_associate(&tid, clos);
1887 isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1895 static int set_pbf_core_power(struct isst_id *id)
1897 struct isst_pbf_info pbf_info;
1898 struct isst_pkg_ctdp pkg_dev;
1904 ret = isst_get_ctdp_levels(id, &pkg_dev);
1906 debug_printf("isst_get_ctdp_levels failed");
1909 debug_printf("Current_level: %d\n", pkg_dev.current_level);
1911 ret = isst_get_pbf_info(id, pkg_dev.current_level, &pbf_info);
1913 debug_printf("isst_get_pbf_info failed");
1916 debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1919 ret = set_core_priority_and_min(id, pbf_info.core_cpumask_size,
1920 pbf_info.core_cpumask,
1921 pbf_info.p1_high, pbf_info.p1_low);
1923 debug_printf("set_core_priority_and_min failed");
1927 ret = isst_pm_qos_config(id, 1, 1);
1929 debug_printf("isst_pm_qos_config failed");
1936 static void set_pbf_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1939 struct isst_pkg_ctdp_level_info ctdp_level;
1940 struct isst_pkg_ctdp pkg_dev;
1942 int status = *(int *)arg4;
1944 if (is_clx_n_platform()) {
1947 set_clx_pbf_cpufreq_scaling_min_max(id);
1950 set_scaling_max_to_cpuinfo_max(id);
1951 set_scaling_min_to_cpuinfo_min(id);
1956 ret = isst_get_ctdp_levels(id, &pkg_dev);
1958 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
1962 ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
1964 isst_display_error_info_message(1, "Failed to get current level", 0, 0);
1968 if (!ctdp_level.pbf_support) {
1969 isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, pkg_dev.current_level);
1974 if (auto_mode && status) {
1975 ret = set_pbf_core_power(id);
1980 ret = isst_set_pbf_fact_status(id, 1, status);
1982 debug_printf("isst_set_pbf_fact_status failed");
1984 isst_pm_qos_config(id, 0, 0);
1988 set_scaling_min_to_cpuinfo_max(id);
1990 set_scaling_min_to_cpuinfo_min(id);
1994 if (auto_mode && !status)
1995 isst_pm_qos_config(id, 0, 1);
1999 isst_display_result(id, outf, "base-freq", "enable",
2002 isst_display_result(id, outf, "base-freq", "disable",
2006 static void set_pbf_enable(int arg)
2013 "Enable Intel Speed Select Technology base frequency feature\n");
2014 if (is_clx_n_platform()) {
2016 "\tOn this platform this command doesn't enable feature in the hardware.\n");
2018 "\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
2023 "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
2026 if (is_clx_n_platform()) {
2028 "\tOn this platform this command doesn't disable feature in the hardware.\n");
2030 "\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
2034 "Disable Intel Speed Select Technology base frequency feature\n");
2036 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
2041 isst_ctdp_display_information_start(outf);
2042 if (max_target_cpus)
2043 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
2046 for_each_online_power_domain_in_set(set_pbf_for_cpu, NULL, NULL,
2048 isst_ctdp_display_information_end(outf);
2051 static void dump_fact_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
2052 void *arg3, void *arg4)
2054 struct isst_fact_info fact_info;
2057 ret = isst_get_fact_info(id, tdp_level, fact_bucket, &fact_info);
2059 isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
2060 isst_ctdp_display_information_end(outf);
2063 isst_fact_display_information(id, outf, tdp_level, fact_bucket,
2064 fact_avx, &fact_info);
2068 static void dump_fact_config(int arg)
2072 "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
2074 "\tArguments: -l|--level : Specify tdp level\n");
2076 "\tArguments: -b|--bucket : Bucket index to dump\n");
2078 "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
2082 if (tdp_level == 0xff) {
2083 isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
2087 isst_ctdp_display_information_start(outf);
2088 if (max_target_cpus)
2089 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
2090 NULL, NULL, NULL, NULL);
2092 for_each_online_power_domain_in_set(dump_fact_config_for_cpu, NULL,
2094 isst_ctdp_display_information_end(outf);
2097 static void set_fact_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2100 struct isst_pkg_ctdp_level_info ctdp_level;
2101 struct isst_pkg_ctdp pkg_dev;
2103 int status = *(int *)arg4;
2105 if (status && no_turbo()) {
2106 isst_display_error_info_message(1, "Turbo mode is disabled", 0, 0);
2111 ret = isst_get_ctdp_levels(id, &pkg_dev);
2113 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
2117 ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
2119 isst_display_error_info_message(1, "Failed to get current level", 0, 0);
2123 if (!ctdp_level.fact_support) {
2124 isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, pkg_dev.current_level);
2130 ret = isst_pm_qos_config(id, 1, 1);
2135 ret = isst_set_pbf_fact_status(id, 0, status);
2137 debug_printf("isst_set_pbf_fact_status failed");
2139 isst_pm_qos_config(id, 0, 0);
2146 struct isst_pkg_ctdp pkg_dev;
2148 ret = isst_get_ctdp_levels(id, &pkg_dev);
2149 if (!ret && id->cpu >= 0)
2150 ret = isst_set_trl(id, fact_trl);
2151 if (ret && auto_mode)
2152 isst_pm_qos_config(id, 0, 0);
2155 isst_pm_qos_config(id, 0, 0);
2160 isst_display_result(id, outf, "turbo-freq", "enable", ret);
2162 fact_enable_fail = ret;
2164 /* Since we modified TRL during Fact enable, restore it */
2165 isst_set_trl_from_current_tdp(id, fact_trl);
2166 isst_display_result(id, outf, "turbo-freq", "disable", ret);
2170 static void set_fact_enable(int arg)
2172 int i, ret, enable = arg;
2178 "Enable Intel Speed Select Technology Turbo frequency feature\n");
2180 "Optional: -t|--trl : Specify turbo ratio limit in hex starting with 0x\n");
2182 "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
2184 "-C|--cpu option as as high priority using core-power feature\n");
2187 "Disable Intel Speed Select Technology turbo frequency feature\n");
2189 "Optional: -t|--trl : Specify turbo ratio limit in hex starting with 0x\n");
2191 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
2196 isst_ctdp_display_information_start(outf);
2197 if (max_target_cpus)
2198 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
2201 for_each_online_power_domain_in_set(set_fact_for_cpu, NULL, NULL,
2204 if (!fact_enable_fail && enable && auto_mode) {
2206 * When we adjust CLOS param, we have to set for siblings also.
2207 * So for the each user specified CPU, also add the sibling
2208 * in the present_cpu_mask.
2210 for (i = 0; i < get_topo_max_cpus(); ++i) {
2211 char buffer[128], sibling_list[128], *cpu_str;
2214 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2217 snprintf(buffer, sizeof(buffer),
2218 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
2220 fd = open(buffer, O_RDONLY);
2224 len = read(fd, sibling_list, sizeof(sibling_list));
2230 sibling_list[127] = '\0';
2231 cpu_str = strtok(sibling_list, ",");
2232 while (cpu_str != NULL) {
2235 sscanf(cpu_str, "%d", &cpu);
2236 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
2237 cpu_str = strtok(NULL, ",");
2241 for (i = 0; i < get_topo_max_cpus(); ++i) {
2244 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
2247 if (is_cpu_online(i) != 1)
2250 set_isst_id(&id, i);
2251 ret = set_clos_param(&id, 0, 0, 0, 0, 0xff);
2255 ret = set_clos_param(&id, 1, 15, 15, 0, 0xff);
2259 ret = set_clos_param(&id, 2, 15, 15, 0, 0xff);
2263 ret = set_clos_param(&id, 3, 15, 15, 0, 0xff);
2267 if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2272 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
2273 ret = isst_clos_associate(&id, clos);
2277 set_isst_id(&id, -1);
2278 isst_display_result(&id, outf, "turbo-freq --auto", "enable", 0);
2281 isst_ctdp_display_information_end(outf);
2286 isst_display_result(&id, outf, "turbo-freq --auto", "enable", ret);
2287 isst_ctdp_display_information_end(outf);
2291 static void enable_clos_qos_config(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2295 int status = *(int *)arg4;
2296 int cp_state, cp_cap;
2298 if (!isst_read_pm_config(id, &cp_state, &cp_cap)) {
2300 isst_display_error_info_message(1, "core-power not supported", 0, 0);
2305 if (is_skx_based_platform())
2306 clos_priority_type = 1;
2308 ret = isst_pm_qos_config(id, status, clos_priority_type);
2310 isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
2313 isst_display_result(id, outf, "core-power", "enable",
2316 isst_display_result(id, outf, "core-power", "disable",
2320 static void set_clos_enable(int arg)
2327 "Enable core-power for a package/die\n");
2328 if (!is_skx_based_platform()) {
2330 "\tClos Enable: Specify priority type with [--priority|-p]\n");
2331 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
2335 "Disable core-power: [No command arguments are required]\n");
2340 if (enable && cpufreq_sysfs_present()) {
2342 "cpufreq subsystem and core-power enable will interfere with each other!\n");
2345 isst_ctdp_display_information_start(outf);
2346 if (max_target_cpus)
2347 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
2348 NULL, NULL, &enable);
2350 for_each_online_power_domain_in_set(enable_clos_qos_config, NULL,
2351 NULL, NULL, &enable);
2352 isst_ctdp_display_information_end(outf);
2355 static void dump_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
2356 void *arg3, void *arg4)
2358 struct isst_clos_config clos_config;
2361 ret = isst_pm_get_clos(id, current_clos, &clos_config);
2363 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
2365 isst_clos_display_information(id, outf, current_clos,
2369 static void dump_clos_config(int arg)
2373 "Print Intel Speed Select Technology core power configuration\n");
2375 "\tArguments: [-c | --clos]: Specify clos id\n");
2378 if (current_clos < 0 || current_clos > 3) {
2379 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2380 isst_ctdp_display_information_end(outf);
2384 isst_ctdp_display_information_start(outf);
2385 if (max_target_cpus)
2386 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
2387 NULL, NULL, NULL, NULL);
2389 for_each_online_power_domain_in_set(dump_clos_config_for_cpu, NULL,
2391 isst_ctdp_display_information_end(outf);
2394 static void get_clos_info_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2397 int enable, ret, prio_type;
2399 ret = isst_clos_get_clos_information(id, &enable, &prio_type);
2401 isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2403 int cp_state, cp_cap;
2405 isst_read_pm_config(id, &cp_state, &cp_cap);
2406 isst_clos_display_clos_information(id, outf, enable, prio_type,
2411 static void dump_clos_info(int arg)
2415 "Print Intel Speed Select Technology core power information\n");
2416 fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2420 isst_ctdp_display_information_start(outf);
2421 if (max_target_cpus)
2422 for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2425 for_each_online_power_domain_in_set(get_clos_info_for_cpu, NULL,
2427 isst_ctdp_display_information_end(outf);
2431 static void set_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2434 struct isst_clos_config clos_config;
2440 clos_config.epp = clos_epp;
2441 clos_config.clos_prop_prio = clos_prop_prio;
2442 clos_config.clos_min = clos_min;
2443 clos_config.clos_max = clos_max;
2444 clos_config.clos_desired = clos_desired;
2445 ret = isst_set_clos(id, current_clos, &clos_config);
2447 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
2449 isst_display_result(id, outf, "core-power", "config", ret);
2452 static void set_clos_config(int arg)
2456 "Set core-power configuration for one of the four clos ids\n");
2458 "\tSpecify targeted clos id with [--clos|-c]\n");
2459 if (!is_skx_based_platform()) {
2460 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
2462 "\tSpecify clos Proportional Priority [--weight|-w]\n");
2464 fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
2465 fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
2469 if (current_clos < 0 || current_clos > 3) {
2470 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2473 if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2474 fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
2477 if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
2479 "clos frequency weight is not specified or invalid, default: 0\n");
2483 fprintf(stderr, "clos min is not specified, default: 0\n");
2487 fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
2491 fprintf(stderr, "clos desired is not supported on this platform\n");
2492 clos_desired = 0x00;
2495 isst_ctdp_display_information_start(outf);
2496 if (max_target_cpus)
2497 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
2500 for_each_online_power_domain_in_set(set_clos_config_for_cpu, NULL,
2502 isst_ctdp_display_information_end(outf);
2505 static void set_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2510 ret = isst_clos_associate(id, current_clos);
2512 debug_printf("isst_clos_associate failed");
2514 isst_display_result(id, outf, "core-power", "assoc", ret);
2517 static void set_clos_assoc(int arg)
2520 fprintf(stderr, "Associate a clos id to a CPU\n");
2522 "\tSpecify targeted clos id with [--clos|-c]\n");
2524 "\tFor example to associate clos 1 to CPU 0: issue\n");
2526 "\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
2530 if (current_clos < 0 || current_clos > 3) {
2531 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2535 isst_ctdp_display_information_start(outf);
2537 if (max_target_cpus)
2538 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
2541 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2543 isst_ctdp_display_information_end(outf);
2546 static void get_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2551 ret = isst_clos_get_assoc_status(id, &clos);
2553 isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
2555 isst_clos_display_assoc_information(id, outf, clos);
2558 static void get_clos_assoc(int arg)
2561 fprintf(stderr, "Get associate clos id to a CPU\n");
2562 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
2566 if (!max_target_cpus) {
2567 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2571 isst_ctdp_display_information_start(outf);
2572 for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2574 isst_ctdp_display_information_end(outf);
2577 static void set_turbo_mode_for_cpu(struct isst_id *id, int status)
2582 base_freq = get_cpufreq_base_freq(id->cpu);
2583 set_cpufreq_scaling_min_max(id->cpu, 1, base_freq);
2585 set_scaling_max_to_cpuinfo_max(id);
2589 isst_display_result(id, outf, "turbo-mode", "disable", 0);
2591 isst_display_result(id, outf, "turbo-mode", "enable", 0);
2595 static void set_turbo_mode(int arg)
2597 int i, disable = arg;
2602 fprintf(stderr, "Set turbo mode disable\n");
2604 fprintf(stderr, "Set turbo mode enable\n");
2608 isst_ctdp_display_information_start(outf);
2610 for (i = 0; i < topo_max_cpus; ++i) {
2614 online = parse_int_file(
2615 1, "/sys/devices/system/cpu/cpu%d/online", i);
2618 1; /* online entry for CPU 0 needs some special configs */
2621 set_isst_id(&id, i);
2622 set_turbo_mode_for_cpu(&id, disable);
2626 isst_ctdp_display_information_end(outf);
2629 static void get_set_trl(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2632 unsigned long long trl;
2633 int set = *(int *)arg4;
2639 if (set && !fact_trl) {
2640 isst_display_error_info_message(1, "Invalid TRL. Specify with [-t|--trl]", 0, 0);
2645 ret = isst_set_trl(id, fact_trl);
2646 isst_display_result(id, outf, "turbo-mode", "set-trl", ret);
2650 ret = isst_get_trl(id, &trl);
2652 isst_display_result(id, outf, "turbo-mode", "get-trl", ret);
2654 isst_trl_display_information(id, outf, trl);
2657 static void process_trl(int arg)
2661 fprintf(stderr, "Set TRL (turbo ratio limits)\n");
2662 fprintf(stderr, "\t t|--trl: Specify turbo ratio limit for setting TRL in hex starting with 0x\n");
2664 fprintf(stderr, "Get TRL (turbo ratio limits)\n");
2669 isst_ctdp_display_information_start(outf);
2670 if (max_target_cpus)
2671 for_each_online_target_cpu_in_set(get_set_trl, NULL,
2674 for_each_online_power_domain_in_set(get_set_trl, NULL,
2676 isst_ctdp_display_information_end(outf);
2679 static struct process_cmd_struct clx_n_cmds[] = {
2680 { "perf-profile", "info", dump_isst_config, 0 },
2681 { "base-freq", "info", dump_pbf_config, 0 },
2682 { "base-freq", "enable", set_pbf_enable, 1 },
2683 { "base-freq", "disable", set_pbf_enable, 0 },
2684 { NULL, NULL, NULL, 0 }
2687 static struct process_cmd_struct isst_cmds[] = {
2688 { "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2689 { "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2690 { "perf-profile", "get-config-version", get_tdp_version, 0 },
2691 { "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2692 { "perf-profile", "get-config-current-level", get_tdp_current_level,
2694 { "perf-profile", "set-config-level", set_tdp_level, 0 },
2695 { "perf-profile", "info", dump_isst_config, 0 },
2696 { "base-freq", "info", dump_pbf_config, 0 },
2697 { "base-freq", "enable", set_pbf_enable, 1 },
2698 { "base-freq", "disable", set_pbf_enable, 0 },
2699 { "turbo-freq", "info", dump_fact_config, 0 },
2700 { "turbo-freq", "enable", set_fact_enable, 1 },
2701 { "turbo-freq", "disable", set_fact_enable, 0 },
2702 { "core-power", "info", dump_clos_info, 0 },
2703 { "core-power", "enable", set_clos_enable, 1 },
2704 { "core-power", "disable", set_clos_enable, 0 },
2705 { "core-power", "config", set_clos_config, 0 },
2706 { "core-power", "get-config", dump_clos_config, 0 },
2707 { "core-power", "assoc", set_clos_assoc, 0 },
2708 { "core-power", "get-assoc", get_clos_assoc, 0 },
2709 { "turbo-mode", "enable", set_turbo_mode, 0 },
2710 { "turbo-mode", "disable", set_turbo_mode, 1 },
2711 { "turbo-mode", "get-trl", process_trl, 0 },
2712 { "turbo-mode", "set-trl", process_trl, 1 },
2713 { NULL, NULL, NULL }
2717 * parse cpuset with following syntax
2718 * 1,2,4..6,8-10 and set bits in cpu_subset
2720 void parse_cpu_command(char *optarg)
2722 unsigned int start, end, invalid_count;
2728 while (next && *next) {
2729 if (*next == '-') /* no negative cpu numbers */
2732 start = strtoul(next, &next, 10);
2734 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2735 target_cpus[max_target_cpus++] = start;
2748 next += 1; /* start range */
2749 } else if (*next == '.') {
2752 next += 1; /* start range */
2757 end = strtoul(next, &next, 10);
2761 while (++start <= end) {
2762 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2763 target_cpus[max_target_cpus++] = start;
2770 else if (*next != '\0')
2774 if (invalid_count) {
2775 isst_ctdp_display_information_start(outf);
2776 isst_display_error_info_message(1, "Too many CPUs in one request: max is", 1, MAX_CPUS_IN_ONE_REQ - 1);
2777 isst_ctdp_display_information_end(outf);
2785 for (i = 0; i < max_target_cpus; ++i)
2786 printf("cpu [%d] in arg\n", target_cpus[i]);
2792 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
2796 static void check_optarg(char *option, int hex)
2799 char *start = optarg;
2802 if (hex && strlen(optarg) < 3) {
2803 /* At least 0x plus one character must be present */
2804 fprintf(stderr, "malformed arguments for:%s [%s]\n", option, optarg);
2809 if (optarg[0] != '0' || tolower(optarg[1]) != 'x') {
2810 fprintf(stderr, "malformed arguments for:%s [%s]\n",
2817 for (i = 0; i < strlen(start); ++i) {
2819 if (!isxdigit(start[i])) {
2820 fprintf(stderr, "malformed arguments for:%s [%s]\n",
2824 } else if (!isdigit(start[i])) {
2825 fprintf(stderr, "malformed arguments for:%s [%s]\n",
2833 static void parse_cmd_args(int argc, int start, char **argv)
2838 static struct option long_options[] = {
2839 { "bucket", required_argument, 0, 'b' },
2840 { "level", required_argument, 0, 'l' },
2841 { "online", required_argument, 0, 'o' },
2842 { "trl-type", required_argument, 0, 'r' },
2843 { "trl", required_argument, 0, 't' },
2844 { "help", no_argument, 0, 'h' },
2845 { "clos", required_argument, 0, 'c' },
2846 { "desired", required_argument, 0, 'd' },
2847 { "epp", required_argument, 0, 'e' },
2848 { "min", required_argument, 0, 'n' },
2849 { "max", required_argument, 0, 'm' },
2850 { "priority", required_argument, 0, 'p' },
2851 { "weight", required_argument, 0, 'w' },
2852 { "auto", no_argument, 0, 'a' },
2856 option_index = start;
2859 while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
2860 long_options, &option_index)) != -1) {
2866 check_optarg("bucket", 0);
2867 fact_bucket = atoi(optarg);
2873 check_optarg("level", 0);
2874 tdp_level = atoi(optarg);
2877 force_online_offline = 1;
2880 check_optarg("trl", 1);
2881 sscanf(optarg, "0x%llx", &fact_trl);
2884 if (!strncmp(optarg, "sse", 3)) {
2886 } else if (!strncmp(optarg, "avx2", 4)) {
2888 } else if (!strncmp(optarg, "avx512", 6)) {
2891 fprintf(outf, "Invalid sse,avx options\n");
2897 check_optarg("clos", 0);
2898 current_clos = atoi(optarg);
2901 check_optarg("desired", 0);
2902 clos_desired = atoi(optarg);
2903 clos_desired /= isst_get_disp_freq_multiplier();
2906 check_optarg("epp", 0);
2907 clos_epp = atoi(optarg);
2908 if (is_skx_based_platform()) {
2909 isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2914 check_optarg("min", 0);
2915 clos_min = atoi(optarg);
2916 clos_min /= isst_get_disp_freq_multiplier();
2919 check_optarg("max", 0);
2920 clos_max = atoi(optarg);
2921 clos_max /= isst_get_disp_freq_multiplier();
2924 check_optarg("priority", 0);
2925 clos_priority_type = atoi(optarg);
2926 if (is_skx_based_platform() && !clos_priority_type) {
2927 isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2932 check_optarg("weight", 0);
2933 clos_prop_prio = atoi(optarg);
2934 if (is_skx_based_platform()) {
2935 isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2940 printf("Unknown option: ignore\n");
2945 printf("Garbage at the end of command: ignore\n");
2948 static void isst_help(void)
2950 printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
2951 performance profiles per system via static and/or dynamic\n\
2952 adjustment of core count, workload, Tjmax, and\n\
2954 printf("\nCommands : For feature=perf-profile\n");
2957 if (!is_clx_n_platform()) {
2958 printf("\tget-lock-status\n");
2959 printf("\tget-config-levels\n");
2960 printf("\tget-config-version\n");
2961 printf("\tget-config-enabled\n");
2962 printf("\tget-config-current-level\n");
2963 printf("\tset-config-level\n");
2967 static void pbf_help(void)
2969 printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
2970 on certain cores (high priority cores) in exchange for lower\n\
2971 base frequency on remaining cores (low priority cores).\n");
2972 printf("\tcommand : info\n");
2973 printf("\tcommand : enable\n");
2974 printf("\tcommand : disable\n");
2977 static void fact_help(void)
2979 printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
2980 limits to cores based on priority.\n");
2981 printf("\nCommand: For feature=turbo-freq\n");
2982 printf("\tcommand : info\n");
2983 printf("\tcommand : enable\n");
2984 printf("\tcommand : disable\n");
2987 static void turbo_mode_help(void)
2989 printf("turbo-mode:\tEnables users to enable/disable turbo mode by adjusting frequency settings. Also allows to get and set turbo ratio limits (TRL).\n");
2990 printf("\tcommand : enable\n");
2991 printf("\tcommand : disable\n");
2992 printf("\tcommand : get-trl\n");
2993 printf("\tcommand : set-trl\n");
2997 static void core_power_help(void)
2999 printf("core-power:\tInterface that allows user to define per core/tile\n\
3001 printf("\nCommands : For feature=core-power\n");
3003 printf("\tenable\n");
3004 printf("\tdisable\n");
3005 printf("\tconfig\n");
3006 printf("\tget-config\n");
3007 printf("\tassoc\n");
3008 printf("\tget-assoc\n");
3011 struct process_cmd_help_struct {
3013 void (*process_fn)(void);
3016 static struct process_cmd_help_struct isst_help_cmds[] = {
3017 { "perf-profile", isst_help },
3018 { "base-freq", pbf_help },
3019 { "turbo-freq", fact_help },
3020 { "core-power", core_power_help },
3021 { "turbo-mode", turbo_mode_help },
3025 static struct process_cmd_help_struct clx_n_help_cmds[] = {
3026 { "perf-profile", isst_help },
3027 { "base-freq", pbf_help },
3031 void process_command(int argc, char **argv,
3032 struct process_cmd_help_struct *help_cmds,
3033 struct process_cmd_struct *cmds)
3035 int i = 0, matched = 0;
3036 char *feature = argv[optind];
3037 char *cmd = argv[optind + 1];
3039 if (!feature || !cmd)
3042 debug_printf("feature name [%s] command [%s]\n", feature, cmd);
3043 if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
3044 while (help_cmds[i].feature) {
3045 if (!strcmp(help_cmds[i].feature, feature)) {
3046 help_cmds[i].process_fn();
3054 while (cmds[i].feature) {
3055 if (!strcmp(cmds[i].feature, feature) &&
3056 !strcmp(cmds[i].command, cmd)) {
3057 parse_cmd_args(argc, optind + 1, argv);
3058 cmds[i].process_fn(cmds[i].arg);
3066 fprintf(stderr, "Invalid command\n");
3069 static void usage(void)
3071 if (is_clx_n_platform()) {
3072 fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
3073 fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
3076 printf("\nUsage:\n");
3077 printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
3078 printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
3079 if (is_clx_n_platform())
3080 printf("\nFEATURE : [perf-profile|base-freq]\n");
3082 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power|turbo-mode]\n");
3083 printf("\nFor help on each feature, use -h|--help\n");
3084 printf("\tFor example: intel-speed-select perf-profile -h\n");
3086 printf("\nFor additional help on each command for a feature, use --h|--help\n");
3087 printf("\tFor example: intel-speed-select perf-profile get-lock-status -h\n");
3088 printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
3090 printf("\nOPTIONS\n");
3091 printf("\t[-c|--cpu] : logical cpu number\n");
3092 printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
3093 printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
3094 printf("\t[-d|--debug] : Debug mode\n");
3095 printf("\t[-f|--format] : output format [json|text]. Default: text\n");
3096 printf("\t[-h|--help] : Print help\n");
3097 printf("\t[-i|--info] : Print platform information\n");
3098 printf("\t[-a|--all-cpus-online] : Force online every CPU in the system\n");
3099 printf("\t[-o|--out] : Output file\n");
3100 printf("\t\t\tDefault : stderr\n");
3101 printf("\t[-p|--pause] : Delay between two mail box commands in milliseconds\n");
3102 printf("\t[-r|--retry] : Retry count for mail box commands on failure, default 3\n");
3103 printf("\t[-v|--version] : Print version\n");
3104 printf("\t[-b|--oob : Start a daemon to process HFI events for perf profile change from Out of Band agent.\n");
3105 printf("\t[-n|--no-daemon : Don't run as daemon. By default --oob will turn on daemon mode\n");
3106 printf("\t[-w|--delay : Delay for reading config level state change in OOB poll mode.\n");
3107 printf("\t[-g|--cgroupv2 : Try to use cgroup v2 CPU isolation instead of CPU online/offline.\n");
3108 printf("\t[-u|--cpu0-workaround : Don't try to online/offline CPU0 instead use cgroup v2.\n");
3109 printf("\nResult format\n");
3110 printf("\tResult display uses a common format for each command:\n");
3111 printf("\tResults are formatted in text/JSON with\n");
3112 printf("\t\tPackage, Die, CPU, and command specific results.\n");
3114 printf("\nExamples\n");
3115 printf("\tTo get platform information:\n");
3116 printf("\t\tintel-speed-select --info\n");
3117 printf("\tTo get full perf-profile information dump:\n");
3118 printf("\t\tintel-speed-select perf-profile info\n");
3119 printf("\tTo get full base-freq information dump:\n");
3120 printf("\t\tintel-speed-select base-freq info -l 0\n");
3121 if (!is_clx_n_platform()) {
3122 printf("\tTo get full turbo-freq information dump:\n");
3123 printf("\t\tintel-speed-select turbo-freq info -l 0\n");
3128 static void print_version(void)
3130 fprintf(outf, "Version %s\n", version_str);
3134 static void cmdline(int argc, char **argv)
3136 const char *pathname = "/dev/isst_interface";
3139 int opt, force_cpus_online = 0;
3140 int option_index = 0;
3143 int poll_interval = -1;
3145 int mbox_delay = 0, mbox_retries = 3;
3147 static struct option long_options[] = {
3148 { "all-cpus-online", no_argument, 0, 'a' },
3149 { "cpu", required_argument, 0, 'c' },
3150 { "debug", no_argument, 0, 'd' },
3151 { "format", required_argument, 0, 'f' },
3152 { "help", no_argument, 0, 'h' },
3153 { "info", no_argument, 0, 'i' },
3154 { "pause", required_argument, 0, 'p' },
3155 { "out", required_argument, 0, 'o' },
3156 { "retry", required_argument, 0, 'r' },
3157 { "version", no_argument, 0, 'v' },
3158 { "oob", no_argument, 0, 'b' },
3159 { "no-daemon", no_argument, 0, 'n' },
3160 { "poll-interval", required_argument, 0, 'w' },
3161 { "cgroupv2", required_argument, 0, 'g' },
3162 { "cpu0-workaround", required_argument, 0, 'u' },
3166 if (geteuid() != 0) {
3167 fprintf(stderr, "Must run as root\n");
3171 ret = update_cpu_model();
3173 err(-1, "Invalid CPU model (%d)\n", cpu_model);
3174 printf("Intel(R) Speed Select Technology\n");
3175 printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
3177 if (!is_clx_n_platform()) {
3178 fp = fopen(pathname, "rb");
3180 fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
3181 fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
3182 fprintf(stderr, "If the config is included then this is not a supported platform.\n");
3188 ret = isst_fill_platform_info();
3193 while ((opt = getopt_long_only(argc, argv, "+c:df:hio:vabw:ngu", long_options,
3194 &option_index)) != -1) {
3197 force_cpus_online = 1;
3200 parse_cpu_command(optarg);
3204 printf("Debug Mode ON\n");
3207 if (!strncmp(optarg, "json", 4))
3208 out_format_json = 1;
3214 isst_print_platform_information();
3219 outf = fopen_or_exit(optarg, "w");
3222 ret = strtol(optarg, &ptr, 10);
3224 fprintf(stderr, "Invalid pause interval, ignore\n");
3229 ret = strtol(optarg, &ptr, 10);
3231 fprintf(stderr, "Invalid retry count, ignore\n");
3245 ret = strtol(optarg, &ptr, 10);
3247 fprintf(stderr, "Invalid poll interval count\n");
3250 poll_interval = ret;
3263 if (optind > (argc - 2) && !oob_mode) {
3268 isst_update_platform_param(ISST_PARAM_MBOX_DELAY, mbox_delay);
3269 isst_update_platform_param(ISST_PARAM_MBOX_RETRIES, mbox_retries);
3272 if (force_cpus_online)
3273 force_all_cpus_online();
3274 store_cpu_topology();
3279 fprintf(stderr, "OOB mode is enabled in debug mode\n");
3281 ret = isst_daemon(debug_flag, poll_interval, no_daemon);
3283 fprintf(stderr, "OOB mode enable failed\n");
3287 if (!is_clx_n_platform()) {
3288 process_command(argc, argv, isst_help_cmds, isst_cmds);
3290 process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
3293 free_cpu_set(present_cpumask);
3294 free_cpu_set(target_cpumask);
3297 int main(int argc, char **argv)
3300 cmdline(argc, argv);