1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Speed Select -- Enumerate and control features
4 * Copyright (c) 2019 Intel Corporation.
7 #include <linux/isst_if.h>
11 struct process_cmd_struct {
14 void (*process_fn)(int arg);
18 static const char *version_str = "v1.2";
19 static const int supported_api_ver = 1;
20 static struct isst_if_platform_info isst_platform_info;
21 static char *progname;
22 static int debug_flag;
26 static int cpu_stepping;
28 #define MAX_CPUS_IN_ONE_REQ 64
29 static short max_target_cpus;
30 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
32 static int topo_max_cpus;
33 static size_t present_cpumask_size;
34 static cpu_set_t *present_cpumask;
35 static size_t target_cpumask_size;
36 static cpu_set_t *target_cpumask;
37 static int tdp_level = 0xFF;
38 static int fact_bucket = 0xFF;
39 static int fact_avx = 0xFF;
40 static unsigned long long fact_trl;
41 static int out_format_json;
43 static int force_online_offline;
45 static int fact_enable_fail;
48 static int current_clos = -1;
49 static int clos_epp = -1;
50 static int clos_prop_prio = -1;
51 static int clos_min = -1;
52 static int clos_max = -1;
53 static int clos_desired = -1;
54 static int clos_priority_type;
57 unsigned short core_id;
58 unsigned short pkg_id;
59 unsigned short die_id;
60 unsigned short punit_cpu;
61 unsigned short punit_cpu_core;
63 struct _cpu_map *cpu_map;
72 FILE *get_output_file(void)
77 void debug_printf(const char *format, ...)
81 va_start(args, format);
84 vprintf(format, args);
90 int is_clx_n_platform(void)
92 if (cpu_model == 0x55)
93 if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
98 int is_skx_based_platform(void)
100 if (cpu_model == 0x55)
106 static int update_cpu_model(void)
108 unsigned int ebx, ecx, edx;
109 unsigned int fms, family;
111 __cpuid(1, fms, ebx, ecx, edx);
112 family = (fms >> 8) & 0xf;
113 cpu_model = (fms >> 4) & 0xf;
114 if (family == 6 || family == 0xf)
115 cpu_model += ((fms >> 16) & 0xf) << 4;
117 cpu_stepping = fms & 0xf;
118 /* only three CascadeLake-N models are supported */
119 if (is_clx_n_platform()) {
125 fp = fopen("/proc/cpuinfo", "r");
127 err(-1, "cannot open /proc/cpuinfo\n");
129 while (getline(&line, &n, fp) > 0) {
130 if (strstr(line, "model name")) {
131 if (strstr(line, "6252N") ||
132 strstr(line, "6230N") ||
133 strstr(line, "5218N"))
145 /* Open a file, and exit on failure */
146 static FILE *fopen_or_exit(const char *path, const char *mode)
148 FILE *filep = fopen(path, mode);
151 err(1, "%s: open failed", path);
156 /* Parse a file containing a single int */
157 static int parse_int_file(int fatal, const char *fmt, ...)
165 vsnprintf(path, sizeof(path), fmt, args);
168 filep = fopen_or_exit(path, "r");
170 filep = fopen(path, "r");
174 if (fscanf(filep, "%d", &value) != 1)
175 err(1, "%s: failed to parse number from file", path);
181 int cpufreq_sysfs_present(void)
185 dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
194 int out_format_is_json(void)
196 return out_format_json;
199 static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
201 const char *pathname = "/tmp/isst_cpu_topology.dat";
202 struct cpu_topology cpu_top;
206 fp = fopen(pathname, "rb");
210 ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
214 ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
220 *pkg_id = cpu_top.pkg_id;
221 *core_id = cpu_top.core_id;
222 *die_id = cpu_top.die_id;
231 static void store_cpu_topology(void)
233 const char *pathname = "/tmp/isst_cpu_topology.dat";
237 fp = fopen(pathname, "rb");
239 /* Mapping already exists */
244 fp = fopen(pathname, "wb");
246 fprintf(stderr, "Can't create file:%s\n", pathname);
250 for (i = 0; i < topo_max_cpus; ++i) {
251 struct cpu_topology cpu_top;
253 cpu_top.core_id = parse_int_file(0,
254 "/sys/devices/system/cpu/cpu%d/topology/core_id", i);
255 if (cpu_top.core_id < 0)
256 cpu_top.core_id = -1;
258 cpu_top.pkg_id = parse_int_file(0,
259 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
260 if (cpu_top.pkg_id < 0)
263 cpu_top.die_id = parse_int_file(0,
264 "/sys/devices/system/cpu/cpu%d/topology/die_id", i);
265 if (cpu_top.die_id < 0)
270 if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
271 fprintf(stderr, "Can't write to:%s\n", pathname);
279 int get_physical_package_id(int cpu)
283 ret = parse_int_file(0,
284 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
287 int core_id, pkg_id, die_id;
289 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
297 int get_physical_core_id(int cpu)
301 ret = parse_int_file(0,
302 "/sys/devices/system/cpu/cpu%d/topology/core_id",
305 int core_id, pkg_id, die_id;
307 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
315 int get_physical_die_id(int cpu)
319 ret = parse_int_file(0,
320 "/sys/devices/system/cpu/cpu%d/topology/die_id",
323 int core_id, pkg_id, die_id;
325 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
336 int get_cpufreq_base_freq(int cpu)
338 return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
341 int get_topo_max_cpus(void)
343 return topo_max_cpus;
346 static void set_cpu_online_offline(int cpu, int state)
351 snprintf(buffer, sizeof(buffer),
352 "/sys/devices/system/cpu/cpu%d/online", cpu);
354 fd = open(buffer, O_WRONLY);
357 fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
358 fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
361 err(-1, "%s open failed", buffer);
365 ret = write(fd, "1\n", 2);
367 ret = write(fd, "0\n", 2);
370 perror("Online/Offline: Operation failed\n");
375 #define MAX_PACKAGE_COUNT 8
376 #define MAX_DIE_PER_PACKAGE 2
377 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
379 void *arg1, void *arg2, void *arg3,
382 int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
383 int pkg_index = 0, i;
385 memset(max_packages, 0xff, sizeof(max_packages));
386 for (i = 0; i < topo_max_cpus; ++i) {
387 int j, online, pkg_id, die_id = 0, skip = 0;
389 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
392 online = parse_int_file(
393 1, "/sys/devices/system/cpu/cpu%d/online", i);
396 1; /* online entry for CPU 0 needs some special configs */
398 die_id = get_physical_die_id(i);
402 pkg_id = parse_int_file(0,
403 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
407 /* Create an unique id for package, die combination to store */
408 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
410 for (j = 0; j < pkg_index; ++j) {
411 if (max_packages[j] == pkg_id) {
417 if (!skip && online && callback) {
418 callback(i, arg1, arg2, arg3, arg4);
419 max_packages[pkg_index++] = pkg_id;
424 static void for_each_online_target_cpu_in_set(
425 void (*callback)(int, void *, void *, void *, void *), void *arg1,
426 void *arg2, void *arg3, void *arg4)
430 for (i = 0; i < topo_max_cpus; ++i) {
433 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
436 online = parse_int_file(
437 1, "/sys/devices/system/cpu/cpu%d/online", i);
440 1; /* online entry for CPU 0 needs some special configs */
442 if (online && callback) {
443 callback(i, arg1, arg2, arg3, arg4);
449 fprintf(stderr, "No valid CPU in the list\n");
452 #define BITMASK_SIZE 32
453 static void set_max_cpu_num(void)
460 for (i = 0; i < 256; ++i) {
463 snprintf(path, sizeof(path),
464 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
465 filep = fopen(path, "r");
471 fprintf(stderr, "Can't get max cpu number\n");
475 while (fscanf(filep, "%lx,", &dummy) == 1)
476 topo_max_cpus += BITMASK_SIZE;
479 debug_printf("max cpus %d\n", topo_max_cpus);
482 size_t alloc_cpu_set(cpu_set_t **cpu_set)
487 _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
488 if (_cpu_set == NULL)
490 size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
491 CPU_ZERO_S(size, _cpu_set);
497 void free_cpu_set(cpu_set_t *cpu_set)
502 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
503 static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
504 static void set_cpu_present_cpu_mask(void)
510 size = alloc_cpu_set(&present_cpumask);
511 present_cpumask_size = size;
512 for (i = 0; i < topo_max_cpus; ++i) {
515 snprintf(buffer, sizeof(buffer),
516 "/sys/devices/system/cpu/cpu%d", i);
517 dir = opendir(buffer);
521 CPU_SET_S(i, size, present_cpumask);
522 die_id = get_physical_die_id(i);
526 pkg_id = get_physical_package_id(i);
528 fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
531 if (pkg_id < MAX_PACKAGE_COUNT &&
532 die_id < MAX_DIE_PER_PACKAGE) {
533 int core_id = get_physical_core_id(i);
535 cpu_cnt[pkg_id][die_id]++;
536 core_mask[pkg_id][die_id] |= (1ULL << core_id);
543 int get_core_count(int pkg_id, int die_id)
547 if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE) {
550 for (i = 0; i < sizeof(long long) * 8; ++i) {
551 if (core_mask[pkg_id][die_id] & (1ULL << i))
559 int get_cpu_count(int pkg_id, int die_id)
561 if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
562 return cpu_cnt[pkg_id][die_id];
567 static void set_cpu_target_cpu_mask(void)
572 size = alloc_cpu_set(&target_cpumask);
573 target_cpumask_size = size;
574 for (i = 0; i < max_target_cpus; ++i) {
575 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
579 CPU_SET_S(target_cpus[i], size, target_cpumask);
583 static void create_cpu_map(void)
585 const char *pathname = "/dev/isst_interface";
587 struct isst_if_cpu_maps map;
589 cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
593 fd = open(pathname, O_RDWR);
595 err(-1, "%s open failed", pathname);
597 for (i = 0; i < topo_max_cpus; ++i) {
598 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
602 map.cpu_map[0].logical_cpu = i;
604 debug_printf(" map logical_cpu:%d\n",
605 map.cpu_map[0].logical_cpu);
606 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
607 perror("ISST_IF_GET_PHY_ID");
608 fprintf(outf, "Error: map logical_cpu:%d\n",
609 map.cpu_map[0].logical_cpu);
612 cpu_map[i].core_id = get_physical_core_id(i);
613 cpu_map[i].pkg_id = get_physical_package_id(i);
614 cpu_map[i].die_id = get_physical_die_id(i);
615 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
616 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
617 1); // shift to get core id
620 "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
621 i, cpu_map[i].core_id, cpu_map[i].die_id,
622 cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
623 cpu_map[i].punit_cpu_core);
630 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
634 for (i = 0; i < topo_max_cpus; ++i) {
635 if (cpu_map[i].pkg_id == pkg_id &&
636 cpu_map[i].die_id == die_id &&
637 cpu_map[i].punit_cpu_core == punit_core_id)
644 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
645 size_t core_cpumask_size,
646 cpu_set_t *core_cpumask, int *cpu_cnt)
652 die_id = get_physical_die_id(cpu);
653 pkg_id = get_physical_package_id(cpu);
655 for (i = 0; i < 64; ++i) {
656 if (core_mask & BIT(i)) {
659 for (j = 0; j < topo_max_cpus; ++j) {
660 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
663 if (cpu_map[j].pkg_id == pkg_id &&
664 cpu_map[j].die_id == die_id &&
665 cpu_map[j].punit_cpu_core == i) {
666 CPU_SET_S(j, core_cpumask_size,
677 int find_phy_core_num(int logical_cpu)
679 if (logical_cpu < topo_max_cpus)
680 return cpu_map[logical_cpu].punit_cpu_core;
685 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
688 struct isst_if_io_regs io_regs;
689 const char *pathname = "/dev/isst_interface";
693 debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
695 fd = open(pathname, O_RDWR);
697 err(-1, "%s open failed", pathname);
699 io_regs.req_count = 1;
700 io_regs.io_reg[0].logical_cpu = cpu;
701 io_regs.io_reg[0].reg = reg;
702 cmd = ISST_IF_IO_CMD;
704 io_regs.io_reg[0].read_write = 1;
705 io_regs.io_reg[0].value = *value;
707 io_regs.io_reg[0].read_write = 0;
710 if (ioctl(fd, cmd, &io_regs) == -1) {
711 if (errno == ENOTTY) {
712 perror("ISST_IF_IO_COMMAND\n");
713 fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
716 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
720 *value = io_regs.io_reg[0].value;
723 "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
724 cpu, reg, write, *value);
732 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
733 unsigned char sub_command, unsigned int parameter,
734 unsigned int req_data, unsigned int *resp)
736 const char *pathname = "/dev/isst_interface";
738 struct isst_if_mbox_cmds mbox_cmds = { 0 };
741 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
742 cpu, command, sub_command, parameter, req_data);
744 if (!is_skx_based_platform() && command == CONFIG_CLOS &&
745 sub_command != CLOS_PM_QOS_CONFIG) {
748 int clos_id, core_id, ret = 0;
750 debug_printf("CPU %d\n", cpu);
752 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
757 switch (sub_command) {
759 core_id = parameter & 0xff;
760 ret = isst_send_mmio_command(
761 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
767 clos_id = parameter & 0x03;
768 ret = isst_send_mmio_command(
769 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
782 mbox_cmds.cmd_count = 1;
783 mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
784 mbox_cmds.mbox_cmd[0].command = command;
785 mbox_cmds.mbox_cmd[0].sub_command = sub_command;
786 mbox_cmds.mbox_cmd[0].parameter = parameter;
787 mbox_cmds.mbox_cmd[0].req_data = req_data;
789 fd = open(pathname, O_RDWR);
791 err(-1, "%s open failed", pathname);
793 if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
794 if (errno == ENOTTY) {
795 perror("ISST_IF_MBOX_COMMAND\n");
796 fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
800 "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
801 cpu, command, sub_command, parameter, req_data, errno);
804 *resp = mbox_cmds.mbox_cmd[0].resp_data;
806 "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
807 cpu, command, sub_command, parameter, req_data, *resp);
815 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
816 unsigned long long *req_resp)
818 struct isst_if_msr_cmds msr_cmds;
819 const char *pathname = "/dev/isst_interface";
822 fd = open(pathname, O_RDWR);
824 err(-1, "%s open failed", pathname);
826 msr_cmds.cmd_count = 1;
827 msr_cmds.msr_cmd[0].logical_cpu = cpu;
828 msr_cmds.msr_cmd[0].msr = msr;
829 msr_cmds.msr_cmd[0].read_write = write;
831 msr_cmds.msr_cmd[0].data = *req_resp;
833 if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
834 perror("ISST_IF_MSR_COMMAD");
835 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
839 *req_resp = msr_cmds.msr_cmd[0].data;
842 "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
843 cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
851 static int isst_fill_platform_info(void)
853 const char *pathname = "/dev/isst_interface";
856 fd = open(pathname, O_RDWR);
858 err(-1, "%s open failed", pathname);
860 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
861 perror("ISST_IF_GET_PLATFORM_INFO");
868 if (isst_platform_info.api_version > supported_api_ver) {
869 printf("Incompatible API versions; Upgrade of tool is required\n");
875 static void isst_print_extended_platform_info(void)
877 int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
878 struct isst_pkg_ctdp_level_info ctdp_level;
879 struct isst_pkg_ctdp pkg_dev;
883 for (i = 0; i < 256; ++i) {
886 snprintf(path, sizeof(path),
887 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
888 filep = fopen(path, "r");
898 ret = isst_get_ctdp_levels(i, &pkg_dev);
902 if (pkg_dev.enabled) {
903 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
905 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
906 fprintf(outf, "Only performance level 0 (base level) is present\n");
910 fprintf(outf, "TDP level change control is locked\n");
912 fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
914 for (j = 0; j <= pkg_dev.levels; ++j) {
915 ret = isst_get_ctdp_control(i, j, &ctdp_level);
919 if (!fact_support && ctdp_level.fact_support)
922 if (!pbf_support && ctdp_level.pbf_support)
927 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
929 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
932 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
934 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
936 ret = isst_read_pm_config(i, &cp_state, &cp_cap);
938 fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
940 fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
943 static void isst_print_platform_information(void)
945 struct isst_if_platform_info platform_info;
946 const char *pathname = "/dev/isst_interface";
949 if (is_clx_n_platform()) {
950 fprintf(stderr, "\nThis option in not supported on this platform\n");
954 fd = open(pathname, O_RDWR);
956 err(-1, "%s open failed", pathname);
958 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
959 perror("ISST_IF_GET_PLATFORM_INFO");
961 fprintf(outf, "Platform: API version : %d\n",
962 platform_info.api_version);
963 fprintf(outf, "Platform: Driver version : %d\n",
964 platform_info.driver_version);
965 fprintf(outf, "Platform: mbox supported : %d\n",
966 platform_info.mbox_supported);
967 fprintf(outf, "Platform: mmio supported : %d\n",
968 platform_info.mmio_supported);
969 isst_print_extended_platform_info();
977 static char *local_str0, *local_str1;
978 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
981 int (*fn_ptr)(int cpu, void *arg);
985 ret = fn_ptr(cpu, arg2);
987 isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
989 isst_ctdp_display_core_info(cpu, outf, arg3,
990 *(unsigned int *)arg4,
991 local_str0, local_str1);
994 #define _get_tdp_level(desc, suffix, object, help, str0, str1) \
995 static void get_tdp_##object(int arg) \
997 struct isst_pkg_ctdp ctdp; \
1001 "Print %s [No command arguments are required]\n", \
1005 local_str0 = str0; \
1006 local_str1 = str1; \
1007 isst_ctdp_display_information_start(outf); \
1008 if (max_target_cpus) \
1009 for_each_online_target_cpu_in_set( \
1010 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix, \
1011 &ctdp, desc, &ctdp.object); \
1013 for_each_online_package_in_set(exec_on_get_ctdp_cpu, \
1014 isst_get_ctdp_##suffix, \
1017 isst_ctdp_display_information_end(outf); \
1020 _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
1021 _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
1022 _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
1023 _get_tdp_level("get-config-current_level", levels, current_level,
1024 "Current TDP Level", NULL, NULL);
1025 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
1027 struct isst_pkg_ctdp clx_n_pkg_dev;
1029 static int clx_n_get_base_ratio(void)
1032 char *begin, *end, *line = NULL;
1037 fp = fopen("/proc/cpuinfo", "r");
1039 err(-1, "cannot open /proc/cpuinfo\n");
1041 while (getline(&line, &n, fp) > 0) {
1042 if (strstr(line, "model name")) {
1043 /* this is true for CascadeLake-N */
1044 begin = strstr(line, "@ ") + 2;
1045 end = strstr(line, "GHz");
1046 strncpy(number, begin, end - begin);
1047 value = atof(number) * 10;
1054 return (int)(value);
1057 static int clx_n_config(int cpu)
1059 int i, ret, pkg_id, die_id;
1060 unsigned long cpu_bf;
1061 struct isst_pkg_ctdp_level_info *ctdp_level;
1062 struct isst_pbf_info *pbf_info;
1064 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1065 pbf_info = &ctdp_level->pbf_info;
1066 ctdp_level->core_cpumask_size =
1067 alloc_cpu_set(&ctdp_level->core_cpumask);
1069 /* find the frequency base ratio */
1070 ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1071 if (ctdp_level->tdp_ratio == 0) {
1072 debug_printf("CLX: cn base ratio is zero\n");
1077 /* find the high and low priority frequencies */
1078 pbf_info->p1_high = 0;
1079 pbf_info->p1_low = ~0;
1081 pkg_id = get_physical_package_id(cpu);
1082 die_id = get_physical_die_id(cpu);
1084 for (i = 0; i < topo_max_cpus; i++) {
1085 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1088 if (pkg_id != get_physical_package_id(i) ||
1089 die_id != get_physical_die_id(i))
1092 CPU_SET_S(i, ctdp_level->core_cpumask_size,
1093 ctdp_level->core_cpumask);
1095 cpu_bf = parse_int_file(1,
1096 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1098 if (cpu_bf > pbf_info->p1_high)
1099 pbf_info->p1_high = cpu_bf;
1100 if (cpu_bf < pbf_info->p1_low)
1101 pbf_info->p1_low = cpu_bf;
1104 if (pbf_info->p1_high == ~0UL) {
1105 debug_printf("CLX: maximum base frequency not set\n");
1110 if (pbf_info->p1_low == 0) {
1111 debug_printf("CLX: minimum base frequency not set\n");
1116 /* convert frequencies back to ratios */
1117 pbf_info->p1_high = pbf_info->p1_high / 100000;
1118 pbf_info->p1_low = pbf_info->p1_low / 100000;
1120 /* create high priority cpu mask */
1121 pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1122 for (i = 0; i < topo_max_cpus; i++) {
1123 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1126 if (pkg_id != get_physical_package_id(i) ||
1127 die_id != get_physical_die_id(i))
1130 cpu_bf = parse_int_file(1,
1131 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1133 cpu_bf = cpu_bf / 100000;
1134 if (cpu_bf == pbf_info->p1_high)
1135 CPU_SET_S(i, pbf_info->core_cpumask_size,
1136 pbf_info->core_cpumask);
1139 /* extra ctdp & pbf struct parameters */
1140 ctdp_level->processed = 1;
1141 ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1142 ctdp_level->pbf_enabled = 1;
1143 ctdp_level->fact_support = 0; /* FACT is never supported */
1144 ctdp_level->fact_enabled = 0;
1149 free_cpu_set(ctdp_level->core_cpumask);
1153 static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1154 void *arg3, void *arg4)
1158 if (tdp_level != 0xff && tdp_level != 0) {
1159 isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1163 ret = clx_n_config(cpu);
1165 debug_printf("clx_n_config failed");
1167 struct isst_pkg_ctdp_level_info *ctdp_level;
1168 struct isst_pbf_info *pbf_info;
1170 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1171 pbf_info = &ctdp_level->pbf_info;
1172 isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1173 free_cpu_set(ctdp_level->core_cpumask);
1174 free_cpu_set(pbf_info->core_cpumask);
1178 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
1179 void *arg3, void *arg4)
1181 struct isst_pkg_ctdp pkg_dev;
1184 memset(&pkg_dev, 0, sizeof(pkg_dev));
1185 ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
1187 isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1188 isst_ctdp_display_information_end(outf);
1191 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
1192 isst_get_process_ctdp_complete(cpu, &pkg_dev);
1196 static void dump_isst_config(int arg)
1202 "Print Intel(R) Speed Select Technology Performance profile configuration\n");
1204 "including base frequency and turbo frequency configurations\n");
1205 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
1207 "\tIf no arguments, dump information for all TDP levels\n");
1211 if (!is_clx_n_platform())
1212 fn = dump_isst_config_for_cpu;
1214 fn = dump_clx_n_config_for_cpu;
1216 isst_ctdp_display_information_start(outf);
1218 if (max_target_cpus)
1219 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1221 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1223 isst_ctdp_display_information_end(outf);
1226 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1231 ret = isst_set_tdp_level(cpu, tdp_level);
1233 isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1234 isst_ctdp_display_information_end(outf);
1237 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
1239 if (force_online_offline) {
1240 struct isst_pkg_ctdp_level_info ctdp_level;
1241 int pkg_id = get_physical_package_id(cpu);
1242 int die_id = get_physical_die_id(cpu);
1244 fprintf(stderr, "Option is set to online/offline\n");
1245 ctdp_level.core_cpumask_size =
1246 alloc_cpu_set(&ctdp_level.core_cpumask);
1247 isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
1248 if (ctdp_level.cpu_count) {
1249 int i, max_cpus = get_topo_max_cpus();
1250 for (i = 0; i < max_cpus; ++i) {
1251 if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
1253 if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
1254 fprintf(stderr, "online cpu %d\n", i);
1255 set_cpu_online_offline(i, 1);
1257 fprintf(stderr, "offline cpu %d\n", i);
1258 set_cpu_online_offline(i, 0);
1266 static void set_tdp_level(int arg)
1269 fprintf(stderr, "Set Config TDP level\n");
1271 "\t Arguments: -l|--level : Specify tdp level\n");
1273 "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1275 "\t online/offline operation has limitations, refer to Linux hotplug documentation\n");
1279 if (tdp_level == 0xff) {
1280 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1283 isst_ctdp_display_information_start(outf);
1284 if (max_target_cpus)
1285 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
1288 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
1290 isst_ctdp_display_information_end(outf);
1293 static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
1294 void *arg3, void *arg4)
1298 ret = clx_n_config(cpu);
1300 isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
1302 struct isst_pkg_ctdp_level_info *ctdp_level;
1303 struct isst_pbf_info *pbf_info;
1305 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1306 pbf_info = &ctdp_level->pbf_info;
1307 isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
1308 free_cpu_set(ctdp_level->core_cpumask);
1309 free_cpu_set(pbf_info->core_cpumask);
1313 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1316 struct isst_pbf_info pbf_info;
1319 ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
1321 isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
1322 isst_ctdp_display_information_end(outf);
1325 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
1326 isst_get_pbf_info_complete(&pbf_info);
1330 static void dump_pbf_config(int arg)
1336 "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
1338 "\tArguments: -l|--level : Specify tdp level\n");
1342 if (tdp_level == 0xff) {
1343 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1347 if (!is_clx_n_platform())
1348 fn = dump_pbf_config_for_cpu;
1350 fn = clx_n_dump_pbf_config_for_cpu;
1352 isst_ctdp_display_information_start(outf);
1354 if (max_target_cpus)
1355 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1357 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1359 isst_ctdp_display_information_end(outf);
1362 static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1364 struct isst_clos_config clos_config;
1367 ret = isst_pm_get_clos(cpu, clos, &clos_config);
1369 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1372 clos_config.clos_min = min;
1373 clos_config.clos_max = max;
1374 clos_config.epp = epp;
1375 clos_config.clos_prop_prio = wt;
1376 ret = isst_set_clos(cpu, clos, &clos_config);
1378 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1385 static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1387 char buffer[128], freq_str[16];
1391 snprintf(buffer, sizeof(buffer),
1392 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1394 snprintf(buffer, sizeof(buffer),
1395 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1397 fd = open(buffer, O_WRONLY);
1401 snprintf(freq_str, sizeof(freq_str), "%d", freq);
1402 len = strlen(freq_str);
1403 ret = write(fd, freq_str, len);
1413 static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1415 struct isst_pkg_ctdp_level_info *ctdp_level;
1416 struct isst_pbf_info *pbf_info;
1417 int i, pkg_id, die_id, freq, freq_high, freq_low;
1420 ret = clx_n_config(cpu);
1422 debug_printf("cpufreq_scaling_min_max failed for CLX");
1426 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1427 pbf_info = &ctdp_level->pbf_info;
1428 freq_high = pbf_info->p1_high * 100000;
1429 freq_low = pbf_info->p1_low * 100000;
1431 pkg_id = get_physical_package_id(cpu);
1432 die_id = get_physical_die_id(cpu);
1433 for (i = 0; i < get_topo_max_cpus(); ++i) {
1434 if (pkg_id != get_physical_package_id(i) ||
1435 die_id != get_physical_die_id(i))
1438 if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1439 pbf_info->core_cpumask))
1444 set_cpufreq_scaling_min_max(i, 1, freq);
1445 set_cpufreq_scaling_min_max(i, 0, freq);
1451 static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1453 char buffer[128], min_freq[16];
1456 if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1460 snprintf(buffer, sizeof(buffer),
1461 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1463 snprintf(buffer, sizeof(buffer),
1464 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1466 fd = open(buffer, O_RDONLY);
1470 len = read(fd, min_freq, sizeof(min_freq));
1477 snprintf(buffer, sizeof(buffer),
1478 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1480 snprintf(buffer, sizeof(buffer),
1481 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1483 fd = open(buffer, O_WRONLY);
1487 len = strlen(min_freq);
1488 ret = write(fd, min_freq, len);
1498 static void set_scaling_min_to_cpuinfo_max(int cpu)
1500 int i, pkg_id, die_id;
1502 pkg_id = get_physical_package_id(cpu);
1503 die_id = get_physical_die_id(cpu);
1504 for (i = 0; i < get_topo_max_cpus(); ++i) {
1505 if (pkg_id != get_physical_package_id(i) ||
1506 die_id != get_physical_die_id(i))
1509 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1513 static void set_scaling_min_to_cpuinfo_min(int cpu)
1515 int i, pkg_id, die_id;
1517 pkg_id = get_physical_package_id(cpu);
1518 die_id = get_physical_die_id(cpu);
1519 for (i = 0; i < get_topo_max_cpus(); ++i) {
1520 if (pkg_id != get_physical_package_id(i) ||
1521 die_id != get_physical_die_id(i))
1524 set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1528 static void set_scaling_max_to_cpuinfo_max(int cpu)
1530 int i, pkg_id, die_id;
1532 pkg_id = get_physical_package_id(cpu);
1533 die_id = get_physical_die_id(cpu);
1534 for (i = 0; i < get_topo_max_cpus(); ++i) {
1535 if (pkg_id != get_physical_package_id(i) ||
1536 die_id != get_physical_die_id(i))
1539 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1543 static int set_core_priority_and_min(int cpu, int mask_size,
1544 cpu_set_t *cpu_mask, int min_high,
1547 int pkg_id, die_id, ret, i;
1549 if (!CPU_COUNT_S(mask_size, cpu_mask))
1552 ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1556 ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1560 ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1564 ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1568 pkg_id = get_physical_package_id(cpu);
1569 die_id = get_physical_die_id(cpu);
1570 for (i = 0; i < get_topo_max_cpus(); ++i) {
1573 if (pkg_id != get_physical_package_id(i) ||
1574 die_id != get_physical_die_id(i))
1577 if (CPU_ISSET_S(i, mask_size, cpu_mask))
1582 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1583 ret = isst_clos_associate(i, clos);
1585 isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1593 static int set_pbf_core_power(int cpu)
1595 struct isst_pbf_info pbf_info;
1596 struct isst_pkg_ctdp pkg_dev;
1599 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1601 debug_printf("isst_get_ctdp_levels failed");
1604 debug_printf("Current_level: %d\n", pkg_dev.current_level);
1606 ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1608 debug_printf("isst_get_pbf_info failed");
1611 debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1614 ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1615 pbf_info.core_cpumask,
1616 pbf_info.p1_high, pbf_info.p1_low);
1618 debug_printf("set_core_priority_and_min failed");
1622 ret = isst_pm_qos_config(cpu, 1, 1);
1624 debug_printf("isst_pm_qos_config failed");
1631 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1635 int status = *(int *)arg4;
1637 if (is_clx_n_platform()) {
1640 set_clx_pbf_cpufreq_scaling_min_max(cpu);
1643 set_scaling_max_to_cpuinfo_max(cpu);
1644 set_scaling_min_to_cpuinfo_min(cpu);
1649 if (auto_mode && status) {
1650 ret = set_pbf_core_power(cpu);
1655 ret = isst_set_pbf_fact_status(cpu, 1, status);
1657 debug_printf("isst_set_pbf_fact_status failed");
1659 isst_pm_qos_config(cpu, 0, 0);
1663 set_scaling_min_to_cpuinfo_max(cpu);
1665 set_scaling_min_to_cpuinfo_min(cpu);
1669 if (auto_mode && !status)
1670 isst_pm_qos_config(cpu, 0, 1);
1674 isst_display_result(cpu, outf, "base-freq", "enable",
1677 isst_display_result(cpu, outf, "base-freq", "disable",
1681 static void set_pbf_enable(int arg)
1688 "Enable Intel Speed Select Technology base frequency feature\n");
1689 if (is_clx_n_platform()) {
1691 "\tOn this platform this command doesn't enable feature in the hardware.\n");
1693 "\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
1698 "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1701 if (is_clx_n_platform()) {
1703 "\tOn this platform this command doesn't disable feature in the hardware.\n");
1705 "\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
1709 "Disable Intel Speed Select Technology base frequency feature\n");
1711 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1716 isst_ctdp_display_information_start(outf);
1717 if (max_target_cpus)
1718 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1721 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1723 isst_ctdp_display_information_end(outf);
1726 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
1727 void *arg3, void *arg4)
1729 struct isst_fact_info fact_info;
1732 ret = isst_get_fact_info(cpu, tdp_level, fact_bucket, &fact_info);
1734 isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1735 isst_ctdp_display_information_end(outf);
1738 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
1739 fact_avx, &fact_info);
1743 static void dump_fact_config(int arg)
1747 "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
1749 "\tArguments: -l|--level : Specify tdp level\n");
1751 "\tArguments: -b|--bucket : Bucket index to dump\n");
1753 "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
1757 if (tdp_level == 0xff) {
1758 isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
1762 isst_ctdp_display_information_start(outf);
1763 if (max_target_cpus)
1764 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
1765 NULL, NULL, NULL, NULL);
1767 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
1769 isst_ctdp_display_information_end(outf);
1772 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1776 int status = *(int *)arg4;
1778 if (auto_mode && status) {
1779 ret = isst_pm_qos_config(cpu, 1, 1);
1784 ret = isst_set_pbf_fact_status(cpu, 0, status);
1786 debug_printf("isst_set_pbf_fact_status failed");
1788 isst_pm_qos_config(cpu, 0, 0);
1795 struct isst_pkg_ctdp pkg_dev;
1797 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1799 ret = isst_set_trl(cpu, fact_trl);
1800 if (ret && auto_mode)
1801 isst_pm_qos_config(cpu, 0, 0);
1804 isst_pm_qos_config(cpu, 0, 0);
1809 isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
1811 fact_enable_fail = ret;
1813 /* Since we modified TRL during Fact enable, restore it */
1814 isst_set_trl_from_current_tdp(cpu, fact_trl);
1815 isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
1819 static void set_fact_enable(int arg)
1821 int i, ret, enable = arg;
1826 "Enable Intel Speed Select Technology Turbo frequency feature\n");
1828 "Optional: -t|--trl : Specify turbo ratio limit\n");
1830 "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1832 "-C|--cpu option as as high priority using core-power feature\n");
1835 "Disable Intel Speed Select Technology turbo frequency feature\n");
1837 "Optional: -t|--trl : Specify turbo ratio limit\n");
1839 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1844 isst_ctdp_display_information_start(outf);
1845 if (max_target_cpus)
1846 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1849 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1851 isst_ctdp_display_information_end(outf);
1853 if (!fact_enable_fail && enable && auto_mode) {
1855 * When we adjust CLOS param, we have to set for siblings also.
1856 * So for the each user specified CPU, also add the sibling
1857 * in the present_cpu_mask.
1859 for (i = 0; i < get_topo_max_cpus(); ++i) {
1860 char buffer[128], sibling_list[128], *cpu_str;
1863 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1866 snprintf(buffer, sizeof(buffer),
1867 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1869 fd = open(buffer, O_RDONLY);
1873 len = read(fd, sibling_list, sizeof(sibling_list));
1879 cpu_str = strtok(sibling_list, ",");
1880 while (cpu_str != NULL) {
1883 sscanf(cpu_str, "%d", &cpu);
1884 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1885 cpu_str = strtok(NULL, ",");
1889 for (i = 0; i < get_topo_max_cpus(); ++i) {
1892 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1895 ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1899 ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
1903 ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
1907 ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
1911 if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1916 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1917 ret = isst_clos_associate(i, clos);
1921 isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
1927 isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1931 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
1935 int status = *(int *)arg4;
1937 if (is_skx_based_platform())
1938 clos_priority_type = 1;
1940 ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1942 isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
1945 isst_display_result(cpu, outf, "core-power", "enable",
1948 isst_display_result(cpu, outf, "core-power", "disable",
1952 static void set_clos_enable(int arg)
1959 "Enable core-power for a package/die\n");
1960 if (!is_skx_based_platform()) {
1962 "\tClos Enable: Specify priority type with [--priority|-p]\n");
1963 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1967 "Disable core-power: [No command arguments are required]\n");
1972 if (enable && cpufreq_sysfs_present()) {
1974 "cpufreq subsystem and core-power enable will interfere with each other!\n");
1977 isst_ctdp_display_information_start(outf);
1978 if (max_target_cpus)
1979 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1980 NULL, NULL, &enable);
1982 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1983 NULL, NULL, &enable);
1984 isst_ctdp_display_information_end(outf);
1987 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1988 void *arg3, void *arg4)
1990 struct isst_clos_config clos_config;
1993 ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1995 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1997 isst_clos_display_information(cpu, outf, current_clos,
2001 static void dump_clos_config(int arg)
2005 "Print Intel Speed Select Technology core power configuration\n");
2007 "\tArguments: [-c | --clos]: Specify clos id\n");
2010 if (current_clos < 0 || current_clos > 3) {
2011 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2012 isst_ctdp_display_information_end(outf);
2016 isst_ctdp_display_information_start(outf);
2017 if (max_target_cpus)
2018 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
2019 NULL, NULL, NULL, NULL);
2021 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
2023 isst_ctdp_display_information_end(outf);
2026 static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2029 int enable, ret, prio_type;
2031 ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
2033 isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2035 int cp_state, cp_cap;
2037 isst_read_pm_config(cpu, &cp_state, &cp_cap);
2038 isst_clos_display_clos_information(cpu, outf, enable, prio_type,
2043 static void dump_clos_info(int arg)
2047 "Print Intel Speed Select Technology core power information\n");
2048 fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2052 isst_ctdp_display_information_start(outf);
2053 if (max_target_cpus)
2054 for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2057 for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2059 isst_ctdp_display_information_end(outf);
2063 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2066 struct isst_clos_config clos_config;
2069 clos_config.pkg_id = get_physical_package_id(cpu);
2070 clos_config.die_id = get_physical_die_id(cpu);
2072 clos_config.epp = clos_epp;
2073 clos_config.clos_prop_prio = clos_prop_prio;
2074 clos_config.clos_min = clos_min;
2075 clos_config.clos_max = clos_max;
2076 clos_config.clos_desired = clos_desired;
2077 ret = isst_set_clos(cpu, current_clos, &clos_config);
2079 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
2081 isst_display_result(cpu, outf, "core-power", "config", ret);
2084 static void set_clos_config(int arg)
2088 "Set core-power configuration for one of the four clos ids\n");
2090 "\tSpecify targeted clos id with [--clos|-c]\n");
2091 if (!is_skx_based_platform()) {
2092 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
2094 "\tSpecify clos Proportional Priority [--weight|-w]\n");
2096 fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
2097 fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
2101 if (current_clos < 0 || current_clos > 3) {
2102 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2105 if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2106 fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
2109 if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
2111 "clos frequency weight is not specified or invalid, default: 0\n");
2115 fprintf(stderr, "clos min is not specified, default: 0\n");
2119 fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
2123 fprintf(stderr, "clos desired is not supported on this platform\n");
2124 clos_desired = 0x00;
2127 isst_ctdp_display_information_start(outf);
2128 if (max_target_cpus)
2129 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
2132 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
2134 isst_ctdp_display_information_end(outf);
2137 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2142 ret = isst_clos_associate(cpu, current_clos);
2144 debug_printf("isst_clos_associate failed");
2146 isst_display_result(cpu, outf, "core-power", "assoc", ret);
2149 static void set_clos_assoc(int arg)
2152 fprintf(stderr, "Associate a clos id to a CPU\n");
2154 "\tSpecify targeted clos id with [--clos|-c]\n");
2156 "\tFor example to associate clos 1 to CPU 0: issue\n");
2158 "\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
2162 if (current_clos < 0 || current_clos > 3) {
2163 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2166 if (max_target_cpus)
2167 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
2170 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2174 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2179 ret = isst_clos_get_assoc_status(cpu, &clos);
2181 isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
2183 isst_clos_display_assoc_information(cpu, outf, clos);
2186 static void get_clos_assoc(int arg)
2189 fprintf(stderr, "Get associate clos id to a CPU\n");
2190 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
2194 if (!max_target_cpus) {
2195 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2199 isst_ctdp_display_information_start(outf);
2200 for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2202 isst_ctdp_display_information_end(outf);
2205 static struct process_cmd_struct clx_n_cmds[] = {
2206 { "perf-profile", "info", dump_isst_config, 0 },
2207 { "base-freq", "info", dump_pbf_config, 0 },
2208 { "base-freq", "enable", set_pbf_enable, 1 },
2209 { "base-freq", "disable", set_pbf_enable, 0 },
2210 { NULL, NULL, NULL, 0 }
2213 static struct process_cmd_struct isst_cmds[] = {
2214 { "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2215 { "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2216 { "perf-profile", "get-config-version", get_tdp_version, 0 },
2217 { "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2218 { "perf-profile", "get-config-current-level", get_tdp_current_level,
2220 { "perf-profile", "set-config-level", set_tdp_level, 0 },
2221 { "perf-profile", "info", dump_isst_config, 0 },
2222 { "base-freq", "info", dump_pbf_config, 0 },
2223 { "base-freq", "enable", set_pbf_enable, 1 },
2224 { "base-freq", "disable", set_pbf_enable, 0 },
2225 { "turbo-freq", "info", dump_fact_config, 0 },
2226 { "turbo-freq", "enable", set_fact_enable, 1 },
2227 { "turbo-freq", "disable", set_fact_enable, 0 },
2228 { "core-power", "info", dump_clos_info, 0 },
2229 { "core-power", "enable", set_clos_enable, 1 },
2230 { "core-power", "disable", set_clos_enable, 0 },
2231 { "core-power", "config", set_clos_config, 0 },
2232 { "core-power", "get-config", dump_clos_config, 0 },
2233 { "core-power", "assoc", set_clos_assoc, 0 },
2234 { "core-power", "get-assoc", get_clos_assoc, 0 },
2235 { NULL, NULL, NULL }
2239 * parse cpuset with following syntax
2240 * 1,2,4..6,8-10 and set bits in cpu_subset
2242 void parse_cpu_command(char *optarg)
2244 unsigned int start, end;
2249 while (next && *next) {
2250 if (*next == '-') /* no negative cpu numbers */
2253 start = strtoul(next, &next, 10);
2255 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2256 target_cpus[max_target_cpus++] = start;
2267 next += 1; /* start range */
2268 } else if (*next == '.') {
2271 next += 1; /* start range */
2276 end = strtoul(next, &next, 10);
2280 while (++start <= end) {
2281 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2282 target_cpus[max_target_cpus++] = start;
2287 else if (*next != '\0')
2295 for (i = 0; i < max_target_cpus; ++i)
2296 printf("cpu [%d] in arg\n", target_cpus[i]);
2302 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
2306 static void parse_cmd_args(int argc, int start, char **argv)
2311 static struct option long_options[] = {
2312 { "bucket", required_argument, 0, 'b' },
2313 { "level", required_argument, 0, 'l' },
2314 { "online", required_argument, 0, 'o' },
2315 { "trl-type", required_argument, 0, 'r' },
2316 { "trl", required_argument, 0, 't' },
2317 { "help", no_argument, 0, 'h' },
2318 { "clos", required_argument, 0, 'c' },
2319 { "desired", required_argument, 0, 'd' },
2320 { "epp", required_argument, 0, 'e' },
2321 { "min", required_argument, 0, 'n' },
2322 { "max", required_argument, 0, 'm' },
2323 { "priority", required_argument, 0, 'p' },
2324 { "weight", required_argument, 0, 'w' },
2325 { "auto", no_argument, 0, 'a' },
2329 option_index = start;
2332 while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
2333 long_options, &option_index)) != -1) {
2339 fact_bucket = atoi(optarg);
2345 tdp_level = atoi(optarg);
2348 force_online_offline = 1;
2351 sscanf(optarg, "0x%llx", &fact_trl);
2354 if (!strncmp(optarg, "sse", 3)) {
2356 } else if (!strncmp(optarg, "avx2", 4)) {
2358 } else if (!strncmp(optarg, "avx512", 6)) {
2361 fprintf(outf, "Invalid sse,avx options\n");
2367 current_clos = atoi(optarg);
2370 clos_desired = atoi(optarg);
2371 clos_desired /= DISP_FREQ_MULTIPLIER;
2374 clos_epp = atoi(optarg);
2375 if (is_skx_based_platform()) {
2376 isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2381 clos_min = atoi(optarg);
2382 clos_min /= DISP_FREQ_MULTIPLIER;
2385 clos_max = atoi(optarg);
2386 clos_max /= DISP_FREQ_MULTIPLIER;
2389 clos_priority_type = atoi(optarg);
2390 if (is_skx_based_platform() && !clos_priority_type) {
2391 isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2396 clos_prop_prio = atoi(optarg);
2397 if (is_skx_based_platform()) {
2398 isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2403 printf("Unknown option: ignore\n");
2408 printf("Garbage at the end of command: ignore\n");
2411 static void isst_help(void)
2413 printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
2414 performance profiles per system via static and/or dynamic\n\
2415 adjustment of core count, workload, Tjmax, and\n\
2417 printf("\nCommands : For feature=perf-profile\n");
2420 if (!is_clx_n_platform()) {
2421 printf("\tget-lock-status\n");
2422 printf("\tget-config-levels\n");
2423 printf("\tget-config-version\n");
2424 printf("\tget-config-enabled\n");
2425 printf("\tget-config-current-level\n");
2426 printf("\tset-config-level\n");
2430 static void pbf_help(void)
2432 printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
2433 on certain cores (high priority cores) in exchange for lower\n\
2434 base frequency on remaining cores (low priority cores).\n");
2435 printf("\tcommand : info\n");
2436 printf("\tcommand : enable\n");
2437 printf("\tcommand : disable\n");
2440 static void fact_help(void)
2442 printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
2443 limits to cores based on priority.\n");
2444 printf("\nCommand: For feature=turbo-freq\n");
2445 printf("\tcommand : info\n");
2446 printf("\tcommand : enable\n");
2447 printf("\tcommand : disable\n");
2450 static void core_power_help(void)
2452 printf("core-power:\tInterface that allows user to define per core/tile\n\
2454 printf("\nCommands : For feature=core-power\n");
2456 printf("\tenable\n");
2457 printf("\tdisable\n");
2458 printf("\tconfig\n");
2459 printf("\tget-config\n");
2460 printf("\tassoc\n");
2461 printf("\tget-assoc\n");
2464 struct process_cmd_help_struct {
2466 void (*process_fn)(void);
2469 static struct process_cmd_help_struct isst_help_cmds[] = {
2470 { "perf-profile", isst_help },
2471 { "base-freq", pbf_help },
2472 { "turbo-freq", fact_help },
2473 { "core-power", core_power_help },
2477 static struct process_cmd_help_struct clx_n_help_cmds[] = {
2478 { "perf-profile", isst_help },
2479 { "base-freq", pbf_help },
2483 void process_command(int argc, char **argv,
2484 struct process_cmd_help_struct *help_cmds,
2485 struct process_cmd_struct *cmds)
2487 int i = 0, matched = 0;
2488 char *feature = argv[optind];
2489 char *cmd = argv[optind + 1];
2491 if (!feature || !cmd)
2494 debug_printf("feature name [%s] command [%s]\n", feature, cmd);
2495 if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2496 while (help_cmds[i].feature) {
2497 if (!strcmp(help_cmds[i].feature, feature)) {
2498 help_cmds[i].process_fn();
2505 if (!is_clx_n_platform())
2509 while (cmds[i].feature) {
2510 if (!strcmp(cmds[i].feature, feature) &&
2511 !strcmp(cmds[i].command, cmd)) {
2512 parse_cmd_args(argc, optind + 1, argv);
2513 cmds[i].process_fn(cmds[i].arg);
2521 fprintf(stderr, "Invalid command\n");
2524 static void usage(void)
2526 if (is_clx_n_platform()) {
2527 fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2528 fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2531 printf("\nUsage:\n");
2532 printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2533 printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2534 if (is_clx_n_platform())
2535 printf("\nFEATURE : [perf-profile|base-freq]\n");
2537 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
2538 printf("\nFor help on each feature, use -h|--help\n");
2539 printf("\tFor example: intel-speed-select perf-profile -h\n");
2541 printf("\nFor additional help on each command for a feature, use --h|--help\n");
2542 printf("\tFor example: intel-speed-select perf-profile get-lock-status -h\n");
2543 printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
2545 printf("\nOPTIONS\n");
2546 printf("\t[-c|--cpu] : logical cpu number\n");
2547 printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
2548 printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
2549 printf("\t[-d|--debug] : Debug mode\n");
2550 printf("\t[-f|--format] : output format [json|text]. Default: text\n");
2551 printf("\t[-h|--help] : Print help\n");
2552 printf("\t[-i|--info] : Print platform information\n");
2553 printf("\t[-o|--out] : Output file\n");
2554 printf("\t\t\tDefault : stderr\n");
2555 printf("\t[-v|--version] : Print version\n");
2557 printf("\nResult format\n");
2558 printf("\tResult display uses a common format for each command:\n");
2559 printf("\tResults are formatted in text/JSON with\n");
2560 printf("\t\tPackage, Die, CPU, and command specific results.\n");
2562 printf("\nExamples\n");
2563 printf("\tTo get platform information:\n");
2564 printf("\t\tintel-speed-select --info\n");
2565 printf("\tTo get full perf-profile information dump:\n");
2566 printf("\t\tintel-speed-select perf-profile info\n");
2567 printf("\tTo get full base-freq information dump:\n");
2568 printf("\t\tintel-speed-select base-freq info -l 0\n");
2569 if (!is_clx_n_platform()) {
2570 printf("\tTo get full turbo-freq information dump:\n");
2571 printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2576 static void print_version(void)
2578 fprintf(outf, "Version %s\n", version_str);
2579 fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
2583 static void cmdline(int argc, char **argv)
2585 const char *pathname = "/dev/isst_interface";
2588 int option_index = 0;
2591 static struct option long_options[] = {
2592 { "cpu", required_argument, 0, 'c' },
2593 { "debug", no_argument, 0, 'd' },
2594 { "format", required_argument, 0, 'f' },
2595 { "help", no_argument, 0, 'h' },
2596 { "info", no_argument, 0, 'i' },
2597 { "out", required_argument, 0, 'o' },
2598 { "version", no_argument, 0, 'v' },
2602 if (geteuid() != 0) {
2603 fprintf(stderr, "Must run as root\n");
2607 ret = update_cpu_model();
2609 err(-1, "Invalid CPU model (%d)\n", cpu_model);
2610 printf("Intel(R) Speed Select Technology\n");
2611 printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2613 if (!is_clx_n_platform()) {
2614 fp = fopen(pathname, "rb");
2616 fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2617 fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2618 fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2625 while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
2626 &option_index)) != -1) {
2629 parse_cpu_command(optarg);
2633 printf("Debug Mode ON\n");
2636 if (!strncmp(optarg, "json", 4))
2637 out_format_json = 1;
2643 isst_print_platform_information();
2648 outf = fopen_or_exit(optarg, "w");
2658 if (optind > (argc - 2)) {
2663 store_cpu_topology();
2664 set_cpu_present_cpu_mask();
2665 set_cpu_target_cpu_mask();
2667 if (!is_clx_n_platform()) {
2668 ret = isst_fill_platform_info();
2671 process_command(argc, argv, isst_help_cmds, isst_cmds);
2673 process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2676 free_cpu_set(present_cpumask);
2677 free_cpu_set(target_cpumask);
2680 int main(int argc, char **argv)
2683 cmdline(argc, argv);