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 static int update_cpu_model(void)
100 unsigned int ebx, ecx, edx;
101 unsigned int fms, family;
103 __cpuid(1, fms, ebx, ecx, edx);
104 family = (fms >> 8) & 0xf;
105 cpu_model = (fms >> 4) & 0xf;
106 if (family == 6 || family == 0xf)
107 cpu_model += ((fms >> 16) & 0xf) << 4;
109 cpu_stepping = fms & 0xf;
110 /* only three CascadeLake-N models are supported */
111 if (is_clx_n_platform()) {
117 fp = fopen("/proc/cpuinfo", "r");
119 err(-1, "cannot open /proc/cpuinfo\n");
121 while (getline(&line, &n, fp) > 0) {
122 if (strstr(line, "model name")) {
123 if (strstr(line, "6252N") ||
124 strstr(line, "6230N") ||
125 strstr(line, "5218N"))
137 /* Open a file, and exit on failure */
138 static FILE *fopen_or_exit(const char *path, const char *mode)
140 FILE *filep = fopen(path, mode);
143 err(1, "%s: open failed", path);
148 /* Parse a file containing a single int */
149 static int parse_int_file(int fatal, const char *fmt, ...)
157 vsnprintf(path, sizeof(path), fmt, args);
160 filep = fopen_or_exit(path, "r");
162 filep = fopen(path, "r");
166 if (fscanf(filep, "%d", &value) != 1)
167 err(1, "%s: failed to parse number from file", path);
173 int cpufreq_sysfs_present(void)
177 dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
186 int out_format_is_json(void)
188 return out_format_json;
191 static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
193 const char *pathname = "/tmp/isst_cpu_topology.dat";
194 struct cpu_topology cpu_top;
198 fp = fopen(pathname, "rb");
202 ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
206 ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
212 *pkg_id = cpu_top.pkg_id;
213 *core_id = cpu_top.core_id;
214 *die_id = cpu_top.die_id;
223 static void store_cpu_topology(void)
225 const char *pathname = "/tmp/isst_cpu_topology.dat";
229 fp = fopen(pathname, "rb");
231 /* Mapping already exists */
236 fp = fopen(pathname, "wb");
238 fprintf(stderr, "Can't create file:%s\n", pathname);
242 for (i = 0; i < topo_max_cpus; ++i) {
243 struct cpu_topology cpu_top;
245 cpu_top.core_id = parse_int_file(0,
246 "/sys/devices/system/cpu/cpu%d/topology/core_id", i);
247 if (cpu_top.core_id < 0)
248 cpu_top.core_id = -1;
250 cpu_top.pkg_id = parse_int_file(0,
251 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
252 if (cpu_top.pkg_id < 0)
255 cpu_top.die_id = parse_int_file(0,
256 "/sys/devices/system/cpu/cpu%d/topology/die_id", i);
257 if (cpu_top.die_id < 0)
262 if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
263 fprintf(stderr, "Can't write to:%s\n", pathname);
271 int get_physical_package_id(int cpu)
275 ret = parse_int_file(0,
276 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
279 int core_id, pkg_id, die_id;
281 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
289 int get_physical_core_id(int cpu)
293 ret = parse_int_file(0,
294 "/sys/devices/system/cpu/cpu%d/topology/core_id",
297 int core_id, pkg_id, die_id;
299 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
307 int get_physical_die_id(int cpu)
311 ret = parse_int_file(0,
312 "/sys/devices/system/cpu/cpu%d/topology/die_id",
315 int core_id, pkg_id, die_id;
317 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
328 int get_cpufreq_base_freq(int cpu)
330 return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
333 int get_topo_max_cpus(void)
335 return topo_max_cpus;
338 static void set_cpu_online_offline(int cpu, int state)
343 snprintf(buffer, sizeof(buffer),
344 "/sys/devices/system/cpu/cpu%d/online", cpu);
346 fd = open(buffer, O_WRONLY);
349 fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
350 fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
353 err(-1, "%s open failed", buffer);
357 ret = write(fd, "1\n", 2);
359 ret = write(fd, "0\n", 2);
362 perror("Online/Offline: Operation failed\n");
367 #define MAX_PACKAGE_COUNT 8
368 #define MAX_DIE_PER_PACKAGE 2
369 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
371 void *arg1, void *arg2, void *arg3,
374 int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
375 int pkg_index = 0, i;
377 memset(max_packages, 0xff, sizeof(max_packages));
378 for (i = 0; i < topo_max_cpus; ++i) {
379 int j, online, pkg_id, die_id = 0, skip = 0;
381 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
384 online = parse_int_file(
385 1, "/sys/devices/system/cpu/cpu%d/online", i);
388 1; /* online entry for CPU 0 needs some special configs */
390 die_id = get_physical_die_id(i);
394 pkg_id = parse_int_file(0,
395 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
399 /* Create an unique id for package, die combination to store */
400 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
402 for (j = 0; j < pkg_index; ++j) {
403 if (max_packages[j] == pkg_id) {
409 if (!skip && online && callback) {
410 callback(i, arg1, arg2, arg3, arg4);
411 max_packages[pkg_index++] = pkg_id;
416 static void for_each_online_target_cpu_in_set(
417 void (*callback)(int, void *, void *, void *, void *), void *arg1,
418 void *arg2, void *arg3, void *arg4)
422 for (i = 0; i < topo_max_cpus; ++i) {
425 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
428 online = parse_int_file(
429 1, "/sys/devices/system/cpu/cpu%d/online", i);
432 1; /* online entry for CPU 0 needs some special configs */
434 if (online && callback)
435 callback(i, arg1, arg2, arg3, arg4);
439 #define BITMASK_SIZE 32
440 static void set_max_cpu_num(void)
447 for (i = 0; i < 256; ++i) {
450 snprintf(path, sizeof(path),
451 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
452 filep = fopen(path, "r");
458 fprintf(stderr, "Can't get max cpu number\n");
462 while (fscanf(filep, "%lx,", &dummy) == 1)
463 topo_max_cpus += BITMASK_SIZE;
466 debug_printf("max cpus %d\n", topo_max_cpus);
469 size_t alloc_cpu_set(cpu_set_t **cpu_set)
474 _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
475 if (_cpu_set == NULL)
477 size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
478 CPU_ZERO_S(size, _cpu_set);
484 void free_cpu_set(cpu_set_t *cpu_set)
489 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
490 static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
491 static void set_cpu_present_cpu_mask(void)
497 size = alloc_cpu_set(&present_cpumask);
498 present_cpumask_size = size;
499 for (i = 0; i < topo_max_cpus; ++i) {
502 snprintf(buffer, sizeof(buffer),
503 "/sys/devices/system/cpu/cpu%d", i);
504 dir = opendir(buffer);
508 CPU_SET_S(i, size, present_cpumask);
509 die_id = get_physical_die_id(i);
513 pkg_id = get_physical_package_id(i);
515 fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
518 if (pkg_id < MAX_PACKAGE_COUNT &&
519 die_id < MAX_DIE_PER_PACKAGE) {
520 int core_id = get_physical_core_id(i);
522 cpu_cnt[pkg_id][die_id]++;
523 core_mask[pkg_id][die_id] |= (1ULL << core_id);
530 int get_core_count(int pkg_id, int die_id)
534 if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE) {
537 for (i = 0; i < sizeof(long long) * 8; ++i) {
538 if (core_mask[pkg_id][die_id] & (1ULL << i))
546 int get_cpu_count(int pkg_id, int die_id)
548 if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
549 return cpu_cnt[pkg_id][die_id];
554 static void set_cpu_target_cpu_mask(void)
559 size = alloc_cpu_set(&target_cpumask);
560 target_cpumask_size = size;
561 for (i = 0; i < max_target_cpus; ++i) {
562 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
566 CPU_SET_S(target_cpus[i], size, target_cpumask);
570 static void create_cpu_map(void)
572 const char *pathname = "/dev/isst_interface";
574 struct isst_if_cpu_maps map;
576 cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
580 fd = open(pathname, O_RDWR);
582 err(-1, "%s open failed", pathname);
584 for (i = 0; i < topo_max_cpus; ++i) {
585 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
589 map.cpu_map[0].logical_cpu = i;
591 debug_printf(" map logical_cpu:%d\n",
592 map.cpu_map[0].logical_cpu);
593 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
594 perror("ISST_IF_GET_PHY_ID");
595 fprintf(outf, "Error: map logical_cpu:%d\n",
596 map.cpu_map[0].logical_cpu);
599 cpu_map[i].core_id = get_physical_core_id(i);
600 cpu_map[i].pkg_id = get_physical_package_id(i);
601 cpu_map[i].die_id = get_physical_die_id(i);
602 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
603 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
604 1); // shift to get core id
607 "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
608 i, cpu_map[i].core_id, cpu_map[i].die_id,
609 cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
610 cpu_map[i].punit_cpu_core);
617 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
621 for (i = 0; i < topo_max_cpus; ++i) {
622 if (cpu_map[i].pkg_id == pkg_id &&
623 cpu_map[i].die_id == die_id &&
624 cpu_map[i].punit_cpu_core == punit_core_id)
631 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
632 size_t core_cpumask_size,
633 cpu_set_t *core_cpumask, int *cpu_cnt)
639 die_id = get_physical_die_id(cpu);
640 pkg_id = get_physical_package_id(cpu);
642 for (i = 0; i < 64; ++i) {
643 if (core_mask & BIT(i)) {
646 for (j = 0; j < topo_max_cpus; ++j) {
647 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
650 if (cpu_map[j].pkg_id == pkg_id &&
651 cpu_map[j].die_id == die_id &&
652 cpu_map[j].punit_cpu_core == i) {
653 CPU_SET_S(j, core_cpumask_size,
664 int find_phy_core_num(int logical_cpu)
666 if (logical_cpu < topo_max_cpus)
667 return cpu_map[logical_cpu].punit_cpu_core;
672 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
675 struct isst_if_io_regs io_regs;
676 const char *pathname = "/dev/isst_interface";
680 debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
682 fd = open(pathname, O_RDWR);
684 err(-1, "%s open failed", pathname);
686 io_regs.req_count = 1;
687 io_regs.io_reg[0].logical_cpu = cpu;
688 io_regs.io_reg[0].reg = reg;
689 cmd = ISST_IF_IO_CMD;
691 io_regs.io_reg[0].read_write = 1;
692 io_regs.io_reg[0].value = *value;
694 io_regs.io_reg[0].read_write = 0;
697 if (ioctl(fd, cmd, &io_regs) == -1) {
698 perror("ISST_IF_IO_CMD");
699 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
703 *value = io_regs.io_reg[0].value;
706 "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
707 cpu, reg, write, *value);
715 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
716 unsigned char sub_command, unsigned int parameter,
717 unsigned int req_data, unsigned int *resp)
719 const char *pathname = "/dev/isst_interface";
721 struct isst_if_mbox_cmds mbox_cmds = { 0 };
724 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
725 cpu, command, sub_command, parameter, req_data);
727 if (isst_platform_info.mmio_supported && command == CONFIG_CLOS &&
728 sub_command != CLOS_PM_QOS_CONFIG) {
731 int clos_id, core_id, ret = 0;
733 debug_printf("CPU %d\n", cpu);
735 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
740 switch (sub_command) {
742 core_id = parameter & 0xff;
743 ret = isst_send_mmio_command(
744 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
750 clos_id = parameter & 0x03;
751 ret = isst_send_mmio_command(
752 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
765 mbox_cmds.cmd_count = 1;
766 mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
767 mbox_cmds.mbox_cmd[0].command = command;
768 mbox_cmds.mbox_cmd[0].sub_command = sub_command;
769 mbox_cmds.mbox_cmd[0].parameter = parameter;
770 mbox_cmds.mbox_cmd[0].req_data = req_data;
772 fd = open(pathname, O_RDWR);
774 err(-1, "%s open failed", pathname);
776 if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
777 perror("ISST_IF_MBOX_COMMAND");
779 "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
780 cpu, command, sub_command, parameter, req_data);
783 *resp = mbox_cmds.mbox_cmd[0].resp_data;
785 "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
786 cpu, command, sub_command, parameter, req_data, *resp);
794 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
795 unsigned long long *req_resp)
797 struct isst_if_msr_cmds msr_cmds;
798 const char *pathname = "/dev/isst_interface";
801 fd = open(pathname, O_RDWR);
803 err(-1, "%s open failed", pathname);
805 msr_cmds.cmd_count = 1;
806 msr_cmds.msr_cmd[0].logical_cpu = cpu;
807 msr_cmds.msr_cmd[0].msr = msr;
808 msr_cmds.msr_cmd[0].read_write = write;
810 msr_cmds.msr_cmd[0].data = *req_resp;
812 if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
813 perror("ISST_IF_MSR_COMMAD");
814 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
818 *req_resp = msr_cmds.msr_cmd[0].data;
821 "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
822 cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
830 static int isst_fill_platform_info(void)
832 const char *pathname = "/dev/isst_interface";
835 fd = open(pathname, O_RDWR);
837 err(-1, "%s open failed", pathname);
839 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
840 perror("ISST_IF_GET_PLATFORM_INFO");
847 if (isst_platform_info.api_version > supported_api_ver) {
848 printf("Incompatible API versions; Upgrade of tool is required\n");
854 static void isst_print_extended_platform_info(void)
856 int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
857 struct isst_pkg_ctdp_level_info ctdp_level;
858 struct isst_pkg_ctdp pkg_dev;
862 for (i = 0; i < 256; ++i) {
865 snprintf(path, sizeof(path),
866 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
867 filep = fopen(path, "r");
877 ret = isst_get_ctdp_levels(i, &pkg_dev);
881 if (pkg_dev.enabled) {
882 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
884 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
885 fprintf(outf, "Only performance level 0 (base level) is present\n");
889 fprintf(outf, "TDP level change control is locked\n");
891 fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
893 for (j = 0; j <= pkg_dev.levels; ++j) {
894 ret = isst_get_ctdp_control(i, j, &ctdp_level);
898 if (!fact_support && ctdp_level.fact_support)
901 if (!pbf_support && ctdp_level.pbf_support)
906 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
908 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
911 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
913 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
915 ret = isst_read_pm_config(i, &cp_state, &cp_cap);
917 fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
919 fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
922 static void isst_print_platform_information(void)
924 struct isst_if_platform_info platform_info;
925 const char *pathname = "/dev/isst_interface";
928 if (is_clx_n_platform()) {
929 fprintf(stderr, "\nThis option in not supported on this platform\n");
933 fd = open(pathname, O_RDWR);
935 err(-1, "%s open failed", pathname);
937 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
938 perror("ISST_IF_GET_PLATFORM_INFO");
940 fprintf(outf, "Platform: API version : %d\n",
941 platform_info.api_version);
942 fprintf(outf, "Platform: Driver version : %d\n",
943 platform_info.driver_version);
944 fprintf(outf, "Platform: mbox supported : %d\n",
945 platform_info.mbox_supported);
946 fprintf(outf, "Platform: mmio supported : %d\n",
947 platform_info.mmio_supported);
948 isst_print_extended_platform_info();
956 static char *local_str0, *local_str1;
957 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
960 int (*fn_ptr)(int cpu, void *arg);
964 ret = fn_ptr(cpu, arg2);
966 isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
968 isst_ctdp_display_core_info(cpu, outf, arg3,
969 *(unsigned int *)arg4,
970 local_str0, local_str1);
973 #define _get_tdp_level(desc, suffix, object, help, str0, str1) \
974 static void get_tdp_##object(int arg) \
976 struct isst_pkg_ctdp ctdp; \
980 "Print %s [No command arguments are required]\n", \
986 isst_ctdp_display_information_start(outf); \
987 if (max_target_cpus) \
988 for_each_online_target_cpu_in_set( \
989 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix, \
990 &ctdp, desc, &ctdp.object); \
992 for_each_online_package_in_set(exec_on_get_ctdp_cpu, \
993 isst_get_ctdp_##suffix, \
996 isst_ctdp_display_information_end(outf); \
999 _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
1000 _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
1001 _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
1002 _get_tdp_level("get-config-current_level", levels, current_level,
1003 "Current TDP Level", NULL, NULL);
1004 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
1006 struct isst_pkg_ctdp clx_n_pkg_dev;
1008 static int clx_n_get_base_ratio(void)
1011 char *begin, *end, *line = NULL;
1016 fp = fopen("/proc/cpuinfo", "r");
1018 err(-1, "cannot open /proc/cpuinfo\n");
1020 while (getline(&line, &n, fp) > 0) {
1021 if (strstr(line, "model name")) {
1022 /* this is true for CascadeLake-N */
1023 begin = strstr(line, "@ ") + 2;
1024 end = strstr(line, "GHz");
1025 strncpy(number, begin, end - begin);
1026 value = atof(number) * 10;
1033 return (int)(value);
1036 static int clx_n_config(int cpu)
1038 int i, ret, pkg_id, die_id;
1039 unsigned long cpu_bf;
1040 struct isst_pkg_ctdp_level_info *ctdp_level;
1041 struct isst_pbf_info *pbf_info;
1043 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1044 pbf_info = &ctdp_level->pbf_info;
1045 ctdp_level->core_cpumask_size =
1046 alloc_cpu_set(&ctdp_level->core_cpumask);
1048 /* find the frequency base ratio */
1049 ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1050 if (ctdp_level->tdp_ratio == 0) {
1051 debug_printf("CLX: cn base ratio is zero\n");
1056 /* find the high and low priority frequencies */
1057 pbf_info->p1_high = 0;
1058 pbf_info->p1_low = ~0;
1060 pkg_id = get_physical_package_id(cpu);
1061 die_id = get_physical_die_id(cpu);
1063 for (i = 0; i < topo_max_cpus; i++) {
1064 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1067 if (pkg_id != get_physical_package_id(i) ||
1068 die_id != get_physical_die_id(i))
1071 CPU_SET_S(i, ctdp_level->core_cpumask_size,
1072 ctdp_level->core_cpumask);
1074 cpu_bf = parse_int_file(1,
1075 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1077 if (cpu_bf > pbf_info->p1_high)
1078 pbf_info->p1_high = cpu_bf;
1079 if (cpu_bf < pbf_info->p1_low)
1080 pbf_info->p1_low = cpu_bf;
1083 if (pbf_info->p1_high == ~0UL) {
1084 debug_printf("CLX: maximum base frequency not set\n");
1089 if (pbf_info->p1_low == 0) {
1090 debug_printf("CLX: minimum base frequency not set\n");
1095 /* convert frequencies back to ratios */
1096 pbf_info->p1_high = pbf_info->p1_high / 100000;
1097 pbf_info->p1_low = pbf_info->p1_low / 100000;
1099 /* create high priority cpu mask */
1100 pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1101 for (i = 0; i < topo_max_cpus; i++) {
1102 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1105 if (pkg_id != get_physical_package_id(i) ||
1106 die_id != get_physical_die_id(i))
1109 cpu_bf = parse_int_file(1,
1110 "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1112 cpu_bf = cpu_bf / 100000;
1113 if (cpu_bf == pbf_info->p1_high)
1114 CPU_SET_S(i, pbf_info->core_cpumask_size,
1115 pbf_info->core_cpumask);
1118 /* extra ctdp & pbf struct parameters */
1119 ctdp_level->processed = 1;
1120 ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1121 ctdp_level->pbf_enabled = 1;
1122 ctdp_level->fact_support = 0; /* FACT is never supported */
1123 ctdp_level->fact_enabled = 0;
1128 free_cpu_set(ctdp_level->core_cpumask);
1132 static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1133 void *arg3, void *arg4)
1137 if (tdp_level != 0xff && tdp_level != 0) {
1138 isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1142 ret = clx_n_config(cpu);
1144 perror("isst_get_process_ctdp");
1146 struct isst_pkg_ctdp_level_info *ctdp_level;
1147 struct isst_pbf_info *pbf_info;
1149 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1150 pbf_info = &ctdp_level->pbf_info;
1151 isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1152 free_cpu_set(ctdp_level->core_cpumask);
1153 free_cpu_set(pbf_info->core_cpumask);
1157 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
1158 void *arg3, void *arg4)
1160 struct isst_pkg_ctdp pkg_dev;
1163 memset(&pkg_dev, 0, sizeof(pkg_dev));
1164 ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
1166 isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1167 isst_ctdp_display_information_end(outf);
1170 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
1171 isst_get_process_ctdp_complete(cpu, &pkg_dev);
1175 static void dump_isst_config(int arg)
1181 "Print Intel(R) Speed Select Technology Performance profile configuration\n");
1183 "including base frequency and turbo frequency configurations\n");
1184 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
1186 "\tIf no arguments, dump information for all TDP levels\n");
1190 if (!is_clx_n_platform())
1191 fn = dump_isst_config_for_cpu;
1193 fn = dump_clx_n_config_for_cpu;
1195 isst_ctdp_display_information_start(outf);
1197 if (max_target_cpus)
1198 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1200 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1202 isst_ctdp_display_information_end(outf);
1205 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1210 ret = isst_set_tdp_level(cpu, tdp_level);
1212 isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1213 isst_ctdp_display_information_end(outf);
1216 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
1218 if (force_online_offline) {
1219 struct isst_pkg_ctdp_level_info ctdp_level;
1220 int pkg_id = get_physical_package_id(cpu);
1221 int die_id = get_physical_die_id(cpu);
1223 fprintf(stderr, "Option is set to online/offline\n");
1224 ctdp_level.core_cpumask_size =
1225 alloc_cpu_set(&ctdp_level.core_cpumask);
1226 isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
1227 if (ctdp_level.cpu_count) {
1228 int i, max_cpus = get_topo_max_cpus();
1229 for (i = 0; i < max_cpus; ++i) {
1230 if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
1232 if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
1233 fprintf(stderr, "online cpu %d\n", i);
1234 set_cpu_online_offline(i, 1);
1236 fprintf(stderr, "offline cpu %d\n", i);
1237 set_cpu_online_offline(i, 0);
1245 static void set_tdp_level(int arg)
1248 fprintf(stderr, "Set Config TDP level\n");
1250 "\t Arguments: -l|--level : Specify tdp level\n");
1252 "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1254 "\t online/offline operation has limitations, refer to Linux hotplug documentation\n");
1258 if (tdp_level == 0xff) {
1259 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1262 isst_ctdp_display_information_start(outf);
1263 if (max_target_cpus)
1264 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
1267 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
1269 isst_ctdp_display_information_end(outf);
1272 static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
1273 void *arg3, void *arg4)
1277 ret = clx_n_config(cpu);
1279 perror("isst_get_process_ctdp");
1281 struct isst_pkg_ctdp_level_info *ctdp_level;
1282 struct isst_pbf_info *pbf_info;
1284 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1285 pbf_info = &ctdp_level->pbf_info;
1286 isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
1287 free_cpu_set(ctdp_level->core_cpumask);
1288 free_cpu_set(pbf_info->core_cpumask);
1292 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1295 struct isst_pbf_info pbf_info;
1298 ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
1300 perror("isst_get_pbf_info");
1302 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
1303 isst_get_pbf_info_complete(&pbf_info);
1307 static void dump_pbf_config(int arg)
1313 "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
1315 "\tArguments: -l|--level : Specify tdp level\n");
1319 if (tdp_level == 0xff) {
1320 fprintf(outf, "Invalid command: specify tdp_level\n");
1324 if (!is_clx_n_platform())
1325 fn = dump_pbf_config_for_cpu;
1327 fn = clx_n_dump_pbf_config_for_cpu;
1329 isst_ctdp_display_information_start(outf);
1331 if (max_target_cpus)
1332 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1334 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1336 isst_ctdp_display_information_end(outf);
1339 static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1341 struct isst_clos_config clos_config;
1344 ret = isst_pm_get_clos(cpu, clos, &clos_config);
1346 perror("isst_pm_get_clos");
1349 clos_config.clos_min = min;
1350 clos_config.clos_max = max;
1351 clos_config.epp = epp;
1352 clos_config.clos_prop_prio = wt;
1353 ret = isst_set_clos(cpu, clos, &clos_config);
1355 perror("isst_pm_set_clos");
1362 static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1364 char buffer[128], freq_str[16];
1368 snprintf(buffer, sizeof(buffer),
1369 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1371 snprintf(buffer, sizeof(buffer),
1372 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1374 fd = open(buffer, O_WRONLY);
1378 snprintf(freq_str, sizeof(freq_str), "%d", freq);
1379 len = strlen(freq_str);
1380 ret = write(fd, freq_str, len);
1390 static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1392 struct isst_pkg_ctdp_level_info *ctdp_level;
1393 struct isst_pbf_info *pbf_info;
1394 int i, pkg_id, die_id, freq, freq_high, freq_low;
1397 ret = clx_n_config(cpu);
1399 perror("set_clx_pbf_cpufreq_scaling_min_max");
1403 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1404 pbf_info = &ctdp_level->pbf_info;
1405 freq_high = pbf_info->p1_high * 100000;
1406 freq_low = pbf_info->p1_low * 100000;
1408 pkg_id = get_physical_package_id(cpu);
1409 die_id = get_physical_die_id(cpu);
1410 for (i = 0; i < get_topo_max_cpus(); ++i) {
1411 if (pkg_id != get_physical_package_id(i) ||
1412 die_id != get_physical_die_id(i))
1415 if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1416 pbf_info->core_cpumask))
1421 set_cpufreq_scaling_min_max(i, 1, freq);
1422 set_cpufreq_scaling_min_max(i, 0, freq);
1428 static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1430 char buffer[128], min_freq[16];
1433 if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1437 snprintf(buffer, sizeof(buffer),
1438 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1440 snprintf(buffer, sizeof(buffer),
1441 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1443 fd = open(buffer, O_RDONLY);
1447 len = read(fd, min_freq, sizeof(min_freq));
1454 snprintf(buffer, sizeof(buffer),
1455 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1457 snprintf(buffer, sizeof(buffer),
1458 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1460 fd = open(buffer, O_WRONLY);
1464 len = strlen(min_freq);
1465 ret = write(fd, min_freq, len);
1475 static void set_scaling_min_to_cpuinfo_max(int cpu)
1477 int i, pkg_id, die_id;
1479 pkg_id = get_physical_package_id(cpu);
1480 die_id = get_physical_die_id(cpu);
1481 for (i = 0; i < get_topo_max_cpus(); ++i) {
1482 if (pkg_id != get_physical_package_id(i) ||
1483 die_id != get_physical_die_id(i))
1486 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1490 static void set_scaling_min_to_cpuinfo_min(int cpu)
1492 int i, pkg_id, die_id;
1494 pkg_id = get_physical_package_id(cpu);
1495 die_id = get_physical_die_id(cpu);
1496 for (i = 0; i < get_topo_max_cpus(); ++i) {
1497 if (pkg_id != get_physical_package_id(i) ||
1498 die_id != get_physical_die_id(i))
1501 set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1505 static void set_scaling_max_to_cpuinfo_max(int cpu)
1507 int i, pkg_id, die_id;
1509 pkg_id = get_physical_package_id(cpu);
1510 die_id = get_physical_die_id(cpu);
1511 for (i = 0; i < get_topo_max_cpus(); ++i) {
1512 if (pkg_id != get_physical_package_id(i) ||
1513 die_id != get_physical_die_id(i))
1516 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1520 static int set_core_priority_and_min(int cpu, int mask_size,
1521 cpu_set_t *cpu_mask, int min_high,
1524 int pkg_id, die_id, ret, i;
1526 if (!CPU_COUNT_S(mask_size, cpu_mask))
1529 ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1533 ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1537 ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1541 ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1545 pkg_id = get_physical_package_id(cpu);
1546 die_id = get_physical_die_id(cpu);
1547 for (i = 0; i < get_topo_max_cpus(); ++i) {
1550 if (pkg_id != get_physical_package_id(i) ||
1551 die_id != get_physical_die_id(i))
1554 if (CPU_ISSET_S(i, mask_size, cpu_mask))
1559 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1560 ret = isst_clos_associate(i, clos);
1562 perror("isst_clos_associate");
1570 static int set_pbf_core_power(int cpu)
1572 struct isst_pbf_info pbf_info;
1573 struct isst_pkg_ctdp pkg_dev;
1576 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1578 perror("isst_get_ctdp_levels");
1581 debug_printf("Current_level: %d\n", pkg_dev.current_level);
1583 ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1585 perror("isst_get_pbf_info");
1588 debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1591 ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1592 pbf_info.core_cpumask,
1593 pbf_info.p1_high, pbf_info.p1_low);
1595 perror("set_core_priority_and_min");
1599 ret = isst_pm_qos_config(cpu, 1, 1);
1601 perror("isst_pm_qos_config");
1608 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1612 int status = *(int *)arg4;
1614 if (is_clx_n_platform()) {
1618 set_clx_pbf_cpufreq_scaling_min_max(cpu);
1623 set_scaling_max_to_cpuinfo_max(cpu);
1624 set_scaling_min_to_cpuinfo_min(cpu);
1630 if (auto_mode && status) {
1631 ret = set_pbf_core_power(cpu);
1636 ret = isst_set_pbf_fact_status(cpu, 1, status);
1638 perror("isst_set_pbf");
1640 isst_pm_qos_config(cpu, 0, 0);
1644 set_scaling_min_to_cpuinfo_max(cpu);
1646 set_scaling_min_to_cpuinfo_min(cpu);
1650 if (auto_mode && !status)
1651 isst_pm_qos_config(cpu, 0, 0);
1655 isst_display_result(cpu, outf, "base-freq", "enable",
1658 isst_display_result(cpu, outf, "base-freq", "disable",
1662 static void set_pbf_enable(int arg)
1669 "Enable Intel Speed Select Technology base frequency feature\n");
1671 "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1675 "Disable Intel Speed Select Technology base frequency feature\n");
1677 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1682 isst_ctdp_display_information_start(outf);
1683 if (max_target_cpus)
1684 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1687 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1689 isst_ctdp_display_information_end(outf);
1692 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
1693 void *arg3, void *arg4)
1695 struct isst_fact_info fact_info;
1698 ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
1700 perror("isst_get_fact_bucket_info");
1702 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
1703 fact_avx, &fact_info);
1706 static void dump_fact_config(int arg)
1710 "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
1712 "\tArguments: -l|--level : Specify tdp level\n");
1714 "\tArguments: -b|--bucket : Bucket index to dump\n");
1716 "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
1720 if (tdp_level == 0xff) {
1721 fprintf(outf, "Invalid command: specify tdp_level\n");
1725 isst_ctdp_display_information_start(outf);
1726 if (max_target_cpus)
1727 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
1728 NULL, NULL, NULL, NULL);
1730 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
1732 isst_ctdp_display_information_end(outf);
1735 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1739 int status = *(int *)arg4;
1741 if (auto_mode && status) {
1742 ret = isst_pm_qos_config(cpu, 1, 1);
1747 ret = isst_set_pbf_fact_status(cpu, 0, status);
1749 perror("isst_set_fact");
1751 isst_pm_qos_config(cpu, 0, 0);
1758 struct isst_pkg_ctdp pkg_dev;
1760 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1762 ret = isst_set_trl(cpu, fact_trl);
1763 if (ret && auto_mode)
1764 isst_pm_qos_config(cpu, 0, 0);
1767 isst_pm_qos_config(cpu, 0, 0);
1772 isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
1774 fact_enable_fail = ret;
1776 /* Since we modified TRL during Fact enable, restore it */
1777 isst_set_trl_from_current_tdp(cpu, fact_trl);
1778 isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
1782 static void set_fact_enable(int arg)
1784 int i, ret, enable = arg;
1789 "Enable Intel Speed Select Technology Turbo frequency feature\n");
1791 "Optional: -t|--trl : Specify turbo ratio limit\n");
1793 "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1795 "-C|--cpu option as as high priority using core-power feature\n");
1798 "Disable Intel Speed Select Technology turbo frequency feature\n");
1800 "Optional: -t|--trl : Specify turbo ratio limit\n");
1802 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1807 isst_ctdp_display_information_start(outf);
1808 if (max_target_cpus)
1809 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1812 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1814 isst_ctdp_display_information_end(outf);
1816 if (!fact_enable_fail && enable && auto_mode) {
1818 * When we adjust CLOS param, we have to set for siblings also.
1819 * So for the each user specified CPU, also add the sibling
1820 * in the present_cpu_mask.
1822 for (i = 0; i < get_topo_max_cpus(); ++i) {
1823 char buffer[128], sibling_list[128], *cpu_str;
1826 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1829 snprintf(buffer, sizeof(buffer),
1830 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1832 fd = open(buffer, O_RDONLY);
1836 len = read(fd, sibling_list, sizeof(sibling_list));
1842 cpu_str = strtok(sibling_list, ",");
1843 while (cpu_str != NULL) {
1846 sscanf(cpu_str, "%d", &cpu);
1847 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1848 cpu_str = strtok(NULL, ",");
1852 for (i = 0; i < get_topo_max_cpus(); ++i) {
1855 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1858 ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1862 ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
1866 ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
1870 ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
1874 if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1879 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1880 ret = isst_clos_associate(i, clos);
1884 isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
1890 isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1894 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
1898 int status = *(int *)arg4;
1900 ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1902 perror("isst_pm_qos_config");
1905 isst_display_result(cpu, outf, "core-power", "enable",
1908 isst_display_result(cpu, outf, "core-power", "disable",
1912 static void set_clos_enable(int arg)
1919 "Enable core-power for a package/die\n");
1921 "\tClos Enable: Specify priority type with [--priority|-p]\n");
1922 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1925 "Disable core-power: [No command arguments are required]\n");
1930 if (enable && cpufreq_sysfs_present()) {
1932 "cpufreq subsystem and core-power enable will interfere with each other!\n");
1935 isst_ctdp_display_information_start(outf);
1936 if (max_target_cpus)
1937 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1938 NULL, NULL, &enable);
1940 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1941 NULL, NULL, &enable);
1942 isst_ctdp_display_information_end(outf);
1945 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1946 void *arg3, void *arg4)
1948 struct isst_clos_config clos_config;
1951 ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1953 perror("isst_pm_get_clos");
1955 isst_clos_display_information(cpu, outf, current_clos,
1959 static void dump_clos_config(int arg)
1963 "Print Intel Speed Select Technology core power configuration\n");
1965 "\tArguments: [-c | --clos]: Specify clos id\n");
1968 if (current_clos < 0 || current_clos > 3) {
1969 fprintf(stderr, "Invalid clos id\n");
1973 isst_ctdp_display_information_start(outf);
1974 if (max_target_cpus)
1975 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
1976 NULL, NULL, NULL, NULL);
1978 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
1980 isst_ctdp_display_information_end(outf);
1983 static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1986 int enable, ret, prio_type;
1988 ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
1990 perror("isst_clos_get_info");
1992 int cp_state, cp_cap;
1994 isst_read_pm_config(cpu, &cp_state, &cp_cap);
1995 isst_clos_display_clos_information(cpu, outf, enable, prio_type,
2000 static void dump_clos_info(int arg)
2004 "Print Intel Speed Select Technology core power information\n");
2005 fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2009 isst_ctdp_display_information_start(outf);
2010 if (max_target_cpus)
2011 for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2014 for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2016 isst_ctdp_display_information_end(outf);
2020 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2023 struct isst_clos_config clos_config;
2026 clos_config.pkg_id = get_physical_package_id(cpu);
2027 clos_config.die_id = get_physical_die_id(cpu);
2029 clos_config.epp = clos_epp;
2030 clos_config.clos_prop_prio = clos_prop_prio;
2031 clos_config.clos_min = clos_min;
2032 clos_config.clos_max = clos_max;
2033 clos_config.clos_desired = clos_desired;
2034 ret = isst_set_clos(cpu, current_clos, &clos_config);
2036 perror("isst_set_clos");
2038 isst_display_result(cpu, outf, "core-power", "config", ret);
2041 static void set_clos_config(int arg)
2045 "Set core-power configuration for one of the four clos ids\n");
2047 "\tSpecify targeted clos id with [--clos|-c]\n");
2048 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
2050 "\tSpecify clos Proportional Priority [--weight|-w]\n");
2051 fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
2052 fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
2053 fprintf(stderr, "\tSpecify clos desired in MHz with [--desired|-d]\n");
2057 if (current_clos < 0 || current_clos > 3) {
2058 fprintf(stderr, "Invalid clos id\n");
2061 if (clos_epp < 0 || clos_epp > 0x0F) {
2062 fprintf(stderr, "clos epp is not specified, default: 0\n");
2065 if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
2067 "clos frequency weight is not specified, default: 0\n");
2071 fprintf(stderr, "clos min is not specified, default: 0\n");
2075 fprintf(stderr, "clos max is not specified, default: 25500 MHz\n");
2078 if (clos_desired < 0) {
2079 fprintf(stderr, "clos desired is not specified, default: 0\n");
2080 clos_desired = 0x00;
2083 isst_ctdp_display_information_start(outf);
2084 if (max_target_cpus)
2085 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
2088 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
2090 isst_ctdp_display_information_end(outf);
2093 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2098 ret = isst_clos_associate(cpu, current_clos);
2100 perror("isst_clos_associate");
2102 isst_display_result(cpu, outf, "core-power", "assoc", ret);
2105 static void set_clos_assoc(int arg)
2108 fprintf(stderr, "Associate a clos id to a CPU\n");
2110 "\tSpecify targeted clos id with [--clos|-c]\n");
2112 "\tFor example to associate clos 1 to CPU 0: issue\n");
2114 "\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
2118 if (current_clos < 0 || current_clos > 3) {
2119 fprintf(stderr, "Invalid clos id\n");
2122 if (max_target_cpus)
2123 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
2127 "Invalid target cpu. Specify with [-c|--cpu]\n");
2131 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2136 ret = isst_clos_get_assoc_status(cpu, &clos);
2138 perror("isst_clos_get_assoc_status");
2140 isst_clos_display_assoc_information(cpu, outf, clos);
2143 static void get_clos_assoc(int arg)
2146 fprintf(stderr, "Get associate clos id to a CPU\n");
2147 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
2151 if (!max_target_cpus) {
2153 "Invalid target cpu. Specify with [-c|--cpu]\n");
2157 isst_ctdp_display_information_start(outf);
2158 for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2160 isst_ctdp_display_information_end(outf);
2163 static struct process_cmd_struct clx_n_cmds[] = {
2164 { "perf-profile", "info", dump_isst_config, 0 },
2165 { "base-freq", "info", dump_pbf_config, 0 },
2166 { "base-freq", "enable", set_pbf_enable, 1 },
2167 { "base-freq", "disable", set_pbf_enable, 0 },
2168 { NULL, NULL, NULL, 0 }
2171 static struct process_cmd_struct isst_cmds[] = {
2172 { "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2173 { "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2174 { "perf-profile", "get-config-version", get_tdp_version, 0 },
2175 { "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2176 { "perf-profile", "get-config-current-level", get_tdp_current_level,
2178 { "perf-profile", "set-config-level", set_tdp_level, 0 },
2179 { "perf-profile", "info", dump_isst_config, 0 },
2180 { "base-freq", "info", dump_pbf_config, 0 },
2181 { "base-freq", "enable", set_pbf_enable, 1 },
2182 { "base-freq", "disable", set_pbf_enable, 0 },
2183 { "turbo-freq", "info", dump_fact_config, 0 },
2184 { "turbo-freq", "enable", set_fact_enable, 1 },
2185 { "turbo-freq", "disable", set_fact_enable, 0 },
2186 { "core-power", "info", dump_clos_info, 0 },
2187 { "core-power", "enable", set_clos_enable, 1 },
2188 { "core-power", "disable", set_clos_enable, 0 },
2189 { "core-power", "config", set_clos_config, 0 },
2190 { "core-power", "get-config", dump_clos_config, 0 },
2191 { "core-power", "assoc", set_clos_assoc, 0 },
2192 { "core-power", "get-assoc", get_clos_assoc, 0 },
2193 { NULL, NULL, NULL }
2197 * parse cpuset with following syntax
2198 * 1,2,4..6,8-10 and set bits in cpu_subset
2200 void parse_cpu_command(char *optarg)
2202 unsigned int start, end;
2207 while (next && *next) {
2208 if (*next == '-') /* no negative cpu numbers */
2211 start = strtoul(next, &next, 10);
2213 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2214 target_cpus[max_target_cpus++] = start;
2225 next += 1; /* start range */
2226 } else if (*next == '.') {
2229 next += 1; /* start range */
2234 end = strtoul(next, &next, 10);
2238 while (++start <= end) {
2239 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2240 target_cpus[max_target_cpus++] = start;
2245 else if (*next != '\0')
2253 for (i = 0; i < max_target_cpus; ++i)
2254 printf("cpu [%d] in arg\n", target_cpus[i]);
2260 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
2264 static void parse_cmd_args(int argc, int start, char **argv)
2269 static struct option long_options[] = {
2270 { "bucket", required_argument, 0, 'b' },
2271 { "level", required_argument, 0, 'l' },
2272 { "online", required_argument, 0, 'o' },
2273 { "trl-type", required_argument, 0, 'r' },
2274 { "trl", required_argument, 0, 't' },
2275 { "help", no_argument, 0, 'h' },
2276 { "clos", required_argument, 0, 'c' },
2277 { "desired", required_argument, 0, 'd' },
2278 { "epp", required_argument, 0, 'e' },
2279 { "min", required_argument, 0, 'n' },
2280 { "max", required_argument, 0, 'm' },
2281 { "priority", required_argument, 0, 'p' },
2282 { "weight", required_argument, 0, 'w' },
2283 { "auto", no_argument, 0, 'a' },
2287 option_index = start;
2290 while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:hoa",
2291 long_options, &option_index)) != -1) {
2297 fact_bucket = atoi(optarg);
2303 tdp_level = atoi(optarg);
2306 force_online_offline = 1;
2309 sscanf(optarg, "0x%llx", &fact_trl);
2312 if (!strncmp(optarg, "sse", 3)) {
2314 } else if (!strncmp(optarg, "avx2", 4)) {
2316 } else if (!strncmp(optarg, "avx512", 4)) {
2319 fprintf(outf, "Invalid sse,avx options\n");
2325 current_clos = atoi(optarg);
2328 clos_desired = atoi(optarg);
2329 clos_desired /= DISP_FREQ_MULTIPLIER;
2332 clos_epp = atoi(optarg);
2335 clos_min = atoi(optarg);
2336 clos_min /= DISP_FREQ_MULTIPLIER;
2339 clos_max = atoi(optarg);
2340 clos_max /= DISP_FREQ_MULTIPLIER;
2343 clos_priority_type = atoi(optarg);
2346 clos_prop_prio = atoi(optarg);
2349 printf("no match\n");
2354 static void isst_help(void)
2356 printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
2357 performance profiles per system via static and/or dynamic\n\
2358 adjustment of core count, workload, Tjmax, and\n\
2360 printf("\nCommands : For feature=perf-profile\n");
2363 if (!is_clx_n_platform()) {
2364 printf("\tget-lock-status\n");
2365 printf("\tget-config-levels\n");
2366 printf("\tget-config-version\n");
2367 printf("\tget-config-enabled\n");
2368 printf("\tget-config-current-level\n");
2369 printf("\tset-config-level\n");
2373 static void pbf_help(void)
2375 printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
2376 on certain cores (high priority cores) in exchange for lower\n\
2377 base frequency on remaining cores (low priority cores).\n");
2378 printf("\tcommand : info\n");
2379 printf("\tcommand : enable\n");
2380 printf("\tcommand : disable\n");
2383 static void fact_help(void)
2385 printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
2386 limits to cores based on priority.\n");
2387 printf("\nCommand: For feature=turbo-freq\n");
2388 printf("\tcommand : info\n");
2389 printf("\tcommand : enable\n");
2390 printf("\tcommand : disable\n");
2393 static void core_power_help(void)
2395 printf("core-power:\tInterface that allows user to define per core/tile\n\
2397 printf("\nCommands : For feature=core-power\n");
2399 printf("\tenable\n");
2400 printf("\tdisable\n");
2401 printf("\tconfig\n");
2402 printf("\tget-config\n");
2403 printf("\tassoc\n");
2404 printf("\tget-assoc\n");
2407 struct process_cmd_help_struct {
2409 void (*process_fn)(void);
2412 static struct process_cmd_help_struct isst_help_cmds[] = {
2413 { "perf-profile", isst_help },
2414 { "base-freq", pbf_help },
2415 { "turbo-freq", fact_help },
2416 { "core-power", core_power_help },
2420 static struct process_cmd_help_struct clx_n_help_cmds[] = {
2421 { "perf-profile", isst_help },
2422 { "base-freq", pbf_help },
2426 void process_command(int argc, char **argv,
2427 struct process_cmd_help_struct *help_cmds,
2428 struct process_cmd_struct *cmds)
2430 int i = 0, matched = 0;
2431 char *feature = argv[optind];
2432 char *cmd = argv[optind + 1];
2434 if (!feature || !cmd)
2437 debug_printf("feature name [%s] command [%s]\n", feature, cmd);
2438 if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2439 while (help_cmds[i].feature) {
2440 if (!strcmp(help_cmds[i].feature, feature)) {
2441 help_cmds[i].process_fn();
2448 if (!is_clx_n_platform())
2452 while (cmds[i].feature) {
2453 if (!strcmp(cmds[i].feature, feature) &&
2454 !strcmp(cmds[i].command, cmd)) {
2455 parse_cmd_args(argc, optind + 1, argv);
2456 cmds[i].process_fn(cmds[i].arg);
2464 fprintf(stderr, "Invalid command\n");
2467 static void usage(void)
2469 if (is_clx_n_platform()) {
2470 fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2471 fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2474 printf("\nUsage:\n");
2475 printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2476 printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2477 if (is_clx_n_platform())
2478 printf("\nFEATURE : [perf-profile|base-freq]\n");
2480 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
2481 printf("\nFor help on each feature, use -h|--help\n");
2482 printf("\tFor example: intel-speed-select perf-profile -h\n");
2484 printf("\nFor additional help on each command for a feature, use --h|--help\n");
2485 printf("\tFor example: intel-speed-select perf-profile get-lock-status -h\n");
2486 printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
2488 printf("\nOPTIONS\n");
2489 printf("\t[-c|--cpu] : logical cpu number\n");
2490 printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
2491 printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
2492 printf("\t[-d|--debug] : Debug mode\n");
2493 printf("\t[-f|--format] : output format [json|text]. Default: text\n");
2494 printf("\t[-h|--help] : Print help\n");
2495 printf("\t[-i|--info] : Print platform information\n");
2496 printf("\t[-o|--out] : Output file\n");
2497 printf("\t\t\tDefault : stderr\n");
2498 printf("\t[-v|--version] : Print version\n");
2500 printf("\nResult format\n");
2501 printf("\tResult display uses a common format for each command:\n");
2502 printf("\tResults are formatted in text/JSON with\n");
2503 printf("\t\tPackage, Die, CPU, and command specific results.\n");
2505 printf("\nExamples\n");
2506 printf("\tTo get platform information:\n");
2507 printf("\t\tintel-speed-select --info\n");
2508 printf("\tTo get full perf-profile information dump:\n");
2509 printf("\t\tintel-speed-select perf-profile info\n");
2510 printf("\tTo get full base-freq information dump:\n");
2511 printf("\t\tintel-speed-select base-freq info -l 0\n");
2512 if (!is_clx_n_platform()) {
2513 printf("\tTo get full turbo-freq information dump:\n");
2514 printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2519 static void print_version(void)
2521 fprintf(outf, "Version %s\n", version_str);
2522 fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
2526 static void cmdline(int argc, char **argv)
2528 const char *pathname = "/dev/isst_interface";
2531 int option_index = 0;
2534 static struct option long_options[] = {
2535 { "cpu", required_argument, 0, 'c' },
2536 { "debug", no_argument, 0, 'd' },
2537 { "format", required_argument, 0, 'f' },
2538 { "help", no_argument, 0, 'h' },
2539 { "info", no_argument, 0, 'i' },
2540 { "out", required_argument, 0, 'o' },
2541 { "version", no_argument, 0, 'v' },
2545 if (geteuid() != 0) {
2546 fprintf(stderr, "Must run as root\n");
2550 ret = update_cpu_model();
2552 err(-1, "Invalid CPU model (%d)\n", cpu_model);
2553 printf("Intel(R) Speed Select Technology\n");
2554 printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2556 if (!is_clx_n_platform()) {
2557 fp = fopen(pathname, "rb");
2559 fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2560 fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2561 fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2568 while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
2569 &option_index)) != -1) {
2572 parse_cpu_command(optarg);
2576 printf("Debug Mode ON\n");
2579 if (!strncmp(optarg, "json", 4))
2580 out_format_json = 1;
2586 isst_print_platform_information();
2591 outf = fopen_or_exit(optarg, "w");
2601 if (optind > (argc - 2)) {
2606 store_cpu_topology();
2607 set_cpu_present_cpu_mask();
2608 set_cpu_target_cpu_mask();
2610 if (!is_clx_n_platform()) {
2611 ret = isst_fill_platform_info();
2614 process_command(argc, argv, isst_help_cmds, isst_cmds);
2616 process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2619 free_cpu_set(present_cpumask);
2620 free_cpu_set(target_cpumask);
2623 int main(int argc, char **argv)
2626 cmdline(argc, argv);