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)(void);
17 static const char *version_str = "v1.0";
18 static const int supported_api_ver = 1;
19 static struct isst_if_platform_info isst_platform_info;
20 static char *progname;
21 static int debug_flag;
26 #define MAX_CPUS_IN_ONE_REQ 64
27 static short max_target_cpus;
28 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
30 static int topo_max_cpus;
31 static size_t present_cpumask_size;
32 static cpu_set_t *present_cpumask;
33 static size_t target_cpumask_size;
34 static cpu_set_t *target_cpumask;
35 static int tdp_level = 0xFF;
36 static int fact_bucket = 0xFF;
37 static int fact_avx = 0xFF;
38 static unsigned long long fact_trl;
39 static int out_format_json;
43 static int current_clos = -1;
44 static int clos_epp = -1;
45 static int clos_prop_prio = -1;
46 static int clos_min = -1;
47 static int clos_max = -1;
48 static int clos_desired = -1;
49 static int clos_priority_type;
52 unsigned short core_id;
53 unsigned short pkg_id;
54 unsigned short die_id;
55 unsigned short punit_cpu;
56 unsigned short punit_cpu_core;
58 struct _cpu_map *cpu_map;
60 void debug_printf(const char *format, ...)
64 va_start(args, format);
67 vprintf(format, args);
72 static void update_cpu_model(void)
74 unsigned int ebx, ecx, edx;
75 unsigned int fms, family;
77 __cpuid(1, fms, ebx, ecx, edx);
78 family = (fms >> 8) & 0xf;
79 cpu_model = (fms >> 4) & 0xf;
80 if (family == 6 || family == 0xf)
81 cpu_model += ((fms >> 16) & 0xf) << 4;
84 /* Open a file, and exit on failure */
85 static FILE *fopen_or_exit(const char *path, const char *mode)
87 FILE *filep = fopen(path, mode);
90 err(1, "%s: open failed", path);
95 /* Parse a file containing a single int */
96 static int parse_int_file(int fatal, const char *fmt, ...)
104 vsnprintf(path, sizeof(path), fmt, args);
107 filep = fopen_or_exit(path, "r");
109 filep = fopen(path, "r");
113 if (fscanf(filep, "%d", &value) != 1)
114 err(1, "%s: failed to parse number from file", path);
120 int cpufreq_sysfs_present(void)
124 dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
133 int out_format_is_json(void)
135 return out_format_json;
138 int get_physical_package_id(int cpu)
140 return parse_int_file(
141 1, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
145 int get_physical_core_id(int cpu)
147 return parse_int_file(
148 1, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
151 int get_physical_die_id(int cpu)
155 ret = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/topology/die_id",
163 int get_topo_max_cpus(void)
165 return topo_max_cpus;
168 #define MAX_PACKAGE_COUNT 8
169 #define MAX_DIE_PER_PACKAGE 2
170 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
172 void *arg1, void *arg2, void *arg3,
175 int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
176 int pkg_index = 0, i;
178 memset(max_packages, 0xff, sizeof(max_packages));
179 for (i = 0; i < topo_max_cpus; ++i) {
180 int j, online, pkg_id, die_id = 0, skip = 0;
182 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
185 online = parse_int_file(
186 1, "/sys/devices/system/cpu/cpu%d/online", i);
189 1; /* online entry for CPU 0 needs some special configs */
191 die_id = get_physical_die_id(i);
194 pkg_id = get_physical_package_id(i);
195 /* Create an unique id for package, die combination to store */
196 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
198 for (j = 0; j < pkg_index; ++j) {
199 if (max_packages[j] == pkg_id) {
205 if (!skip && online && callback) {
206 callback(i, arg1, arg2, arg3, arg4);
207 max_packages[pkg_index++] = pkg_id;
212 static void for_each_online_target_cpu_in_set(
213 void (*callback)(int, void *, void *, void *, void *), void *arg1,
214 void *arg2, void *arg3, void *arg4)
218 for (i = 0; i < topo_max_cpus; ++i) {
221 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
224 online = parse_int_file(
225 1, "/sys/devices/system/cpu/cpu%d/online", i);
228 1; /* online entry for CPU 0 needs some special configs */
230 if (online && callback)
231 callback(i, arg1, arg2, arg3, arg4);
235 #define BITMASK_SIZE 32
236 static void set_max_cpu_num(void)
242 filep = fopen_or_exit(
243 "/sys/devices/system/cpu/cpu0/topology/thread_siblings", "r");
244 while (fscanf(filep, "%lx,", &dummy) == 1)
245 topo_max_cpus += BITMASK_SIZE;
247 topo_max_cpus--; /* 0 based */
249 debug_printf("max cpus %d\n", topo_max_cpus);
252 size_t alloc_cpu_set(cpu_set_t **cpu_set)
257 _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
258 if (_cpu_set == NULL)
260 size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
261 CPU_ZERO_S(size, _cpu_set);
267 void free_cpu_set(cpu_set_t *cpu_set)
272 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
273 static void set_cpu_present_cpu_mask(void)
279 size = alloc_cpu_set(&present_cpumask);
280 present_cpumask_size = size;
281 for (i = 0; i < topo_max_cpus; ++i) {
284 snprintf(buffer, sizeof(buffer),
285 "/sys/devices/system/cpu/cpu%d", i);
286 dir = opendir(buffer);
290 CPU_SET_S(i, size, present_cpumask);
291 die_id = get_physical_die_id(i);
295 pkg_id = get_physical_package_id(i);
296 if (pkg_id < MAX_PACKAGE_COUNT &&
297 die_id < MAX_DIE_PER_PACKAGE)
298 cpu_cnt[pkg_id][die_id]++;
304 int get_cpu_count(int pkg_id, int die_id)
306 if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
307 return cpu_cnt[pkg_id][die_id] + 1;
312 static void set_cpu_target_cpu_mask(void)
317 size = alloc_cpu_set(&target_cpumask);
318 target_cpumask_size = size;
319 for (i = 0; i < max_target_cpus; ++i) {
320 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
324 CPU_SET_S(target_cpus[i], size, target_cpumask);
328 static void create_cpu_map(void)
330 const char *pathname = "/dev/isst_interface";
332 struct isst_if_cpu_maps map;
334 cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
338 fd = open(pathname, O_RDWR);
340 err(-1, "%s open failed", pathname);
342 for (i = 0; i < topo_max_cpus; ++i) {
343 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
347 map.cpu_map[0].logical_cpu = i;
349 debug_printf(" map logical_cpu:%d\n",
350 map.cpu_map[0].logical_cpu);
351 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
352 perror("ISST_IF_GET_PHY_ID");
353 fprintf(outf, "Error: map logical_cpu:%d\n",
354 map.cpu_map[0].logical_cpu);
357 cpu_map[i].core_id = get_physical_core_id(i);
358 cpu_map[i].pkg_id = get_physical_package_id(i);
359 cpu_map[i].die_id = get_physical_die_id(i);
360 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
361 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
362 1); // shift to get core id
365 "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
366 i, cpu_map[i].core_id, cpu_map[i].die_id,
367 cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
368 cpu_map[i].punit_cpu_core);
375 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
379 for (i = 0; i < topo_max_cpus; ++i) {
380 if (cpu_map[i].pkg_id == pkg_id &&
381 cpu_map[i].die_id == die_id &&
382 cpu_map[i].punit_cpu_core == punit_core_id)
389 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
390 size_t core_cpumask_size,
391 cpu_set_t *core_cpumask, int *cpu_cnt)
397 die_id = get_physical_die_id(cpu);
398 pkg_id = get_physical_package_id(cpu);
400 for (i = 0; i < 64; ++i) {
401 if (core_mask & BIT(i)) {
404 for (j = 0; j < topo_max_cpus; ++j) {
405 if (cpu_map[j].pkg_id == pkg_id &&
406 cpu_map[j].die_id == die_id &&
407 cpu_map[j].punit_cpu_core == i) {
408 CPU_SET_S(j, core_cpumask_size,
419 int find_phy_core_num(int logical_cpu)
421 if (logical_cpu < topo_max_cpus)
422 return cpu_map[logical_cpu].punit_cpu_core;
427 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
430 struct isst_if_io_regs io_regs;
431 const char *pathname = "/dev/isst_interface";
435 debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
437 fd = open(pathname, O_RDWR);
439 err(-1, "%s open failed", pathname);
441 io_regs.req_count = 1;
442 io_regs.io_reg[0].logical_cpu = cpu;
443 io_regs.io_reg[0].reg = reg;
444 cmd = ISST_IF_IO_CMD;
446 io_regs.io_reg[0].read_write = 1;
447 io_regs.io_reg[0].value = *value;
449 io_regs.io_reg[0].read_write = 0;
452 if (ioctl(fd, cmd, &io_regs) == -1) {
453 perror("ISST_IF_IO_CMD");
454 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
458 *value = io_regs.io_reg[0].value;
461 "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
462 cpu, reg, write, *value);
470 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
471 unsigned char sub_command, unsigned int parameter,
472 unsigned int req_data, unsigned int *resp)
474 const char *pathname = "/dev/isst_interface";
476 struct isst_if_mbox_cmds mbox_cmds = { 0 };
479 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
480 cpu, command, sub_command, parameter, req_data);
482 if (isst_platform_info.mmio_supported && command == CONFIG_CLOS) {
485 int clos_id, core_id, ret = 0;
487 debug_printf("CLOS %d\n", cpu);
489 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
494 switch (sub_command) {
496 core_id = parameter & 0xff;
497 ret = isst_send_mmio_command(
498 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
504 clos_id = parameter & 0x03;
505 ret = isst_send_mmio_command(
506 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
511 case CLOS_PM_QOS_CONFIG:
512 ret = isst_send_mmio_command(cpu, PM_QOS_CONFIG_OFFSET,
525 mbox_cmds.cmd_count = 1;
526 mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
527 mbox_cmds.mbox_cmd[0].command = command;
528 mbox_cmds.mbox_cmd[0].sub_command = sub_command;
529 mbox_cmds.mbox_cmd[0].parameter = parameter;
530 mbox_cmds.mbox_cmd[0].req_data = req_data;
532 fd = open(pathname, O_RDWR);
534 err(-1, "%s open failed", pathname);
536 if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
537 perror("ISST_IF_MBOX_COMMAND");
539 "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
540 cpu, command, sub_command, parameter, req_data);
542 *resp = mbox_cmds.mbox_cmd[0].resp_data;
544 "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
545 cpu, command, sub_command, parameter, req_data, *resp);
553 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
554 unsigned long long *req_resp)
556 struct isst_if_msr_cmds msr_cmds;
557 const char *pathname = "/dev/isst_interface";
560 fd = open(pathname, O_RDWR);
562 err(-1, "%s open failed", pathname);
564 msr_cmds.cmd_count = 1;
565 msr_cmds.msr_cmd[0].logical_cpu = cpu;
566 msr_cmds.msr_cmd[0].msr = msr;
567 msr_cmds.msr_cmd[0].read_write = write;
569 msr_cmds.msr_cmd[0].data = *req_resp;
571 if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
572 perror("ISST_IF_MSR_COMMAD");
573 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
577 *req_resp = msr_cmds.msr_cmd[0].data;
580 "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
581 cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
589 static int isst_fill_platform_info(void)
591 const char *pathname = "/dev/isst_interface";
594 fd = open(pathname, O_RDWR);
596 err(-1, "%s open failed", pathname);
598 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
599 perror("ISST_IF_GET_PLATFORM_INFO");
609 static void isst_print_platform_information(void)
611 struct isst_if_platform_info platform_info;
612 const char *pathname = "/dev/isst_interface";
615 fd = open(pathname, O_RDWR);
617 err(-1, "%s open failed", pathname);
619 if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
620 perror("ISST_IF_GET_PLATFORM_INFO");
622 fprintf(outf, "Platform: API version : %d\n",
623 platform_info.api_version);
624 fprintf(outf, "Platform: Driver version : %d\n",
625 platform_info.driver_version);
626 fprintf(outf, "Platform: mbox supported : %d\n",
627 platform_info.mbox_supported);
628 fprintf(outf, "Platform: mmio supported : %d\n",
629 platform_info.mmio_supported);
637 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
640 int (*fn_ptr)(int cpu, void *arg);
644 ret = fn_ptr(cpu, arg2);
648 isst_display_result(cpu, outf, "perf-profile", (char *)arg3,
649 *(unsigned int *)arg4);
652 #define _get_tdp_level(desc, suffix, object, help) \
653 static void get_tdp_##object(void) \
655 struct isst_pkg_ctdp ctdp; \
659 "Print %s [No command arguments are required]\n", \
663 isst_ctdp_display_information_start(outf); \
664 if (max_target_cpus) \
665 for_each_online_target_cpu_in_set( \
666 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix, \
667 &ctdp, desc, &ctdp.object); \
669 for_each_online_package_in_set(exec_on_get_ctdp_cpu, \
670 isst_get_ctdp_##suffix, \
673 isst_ctdp_display_information_end(outf); \
676 _get_tdp_level("get-config-levels", levels, levels, "TDP levels");
677 _get_tdp_level("get-config-version", levels, version, "TDP version");
678 _get_tdp_level("get-config-enabled", levels, enabled, "TDP enable status");
679 _get_tdp_level("get-config-current_level", levels, current_level,
680 "Current TDP Level");
681 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status");
683 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
684 void *arg3, void *arg4)
686 struct isst_pkg_ctdp pkg_dev;
689 memset(&pkg_dev, 0, sizeof(pkg_dev));
690 ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
692 perror("isst_get_process_ctdp");
694 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
695 isst_get_process_ctdp_complete(cpu, &pkg_dev);
699 static void dump_isst_config(void)
703 "Print Intel(R) Speed Select Technology Performance profile configuration\n");
705 "including base frequency and turbo frequency configurations\n");
706 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
708 "\tIf no arguments, dump information for all TDP levels\n");
712 isst_ctdp_display_information_start(outf);
715 for_each_online_target_cpu_in_set(dump_isst_config_for_cpu,
716 NULL, NULL, NULL, NULL);
718 for_each_online_package_in_set(dump_isst_config_for_cpu, NULL,
721 isst_ctdp_display_information_end(outf);
724 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
729 ret = isst_set_tdp_level(cpu, tdp_level);
731 perror("set_tdp_level_for_cpu");
733 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
737 static void set_tdp_level(void)
740 fprintf(stderr, "Set Config TDP level\n");
742 "\t Arguments: -l|--level : Specify tdp level\n");
746 if (tdp_level == 0xff) {
747 fprintf(outf, "Invalid command: specify tdp_level\n");
750 isst_ctdp_display_information_start(outf);
752 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
755 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
757 isst_ctdp_display_information_end(outf);
760 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
763 struct isst_pbf_info pbf_info;
766 ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
768 perror("isst_get_pbf_info");
770 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
771 isst_get_pbf_info_complete(&pbf_info);
775 static void dump_pbf_config(void)
779 "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
781 "\tArguments: -l|--level : Specify tdp level\n");
785 if (tdp_level == 0xff) {
786 fprintf(outf, "Invalid command: specify tdp_level\n");
790 isst_ctdp_display_information_start(outf);
792 for_each_online_target_cpu_in_set(dump_pbf_config_for_cpu, NULL,
795 for_each_online_package_in_set(dump_pbf_config_for_cpu, NULL,
797 isst_ctdp_display_information_end(outf);
800 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
804 int status = *(int *)arg4;
806 ret = isst_set_pbf_fact_status(cpu, 1, status);
808 perror("isst_set_pbf");
811 isst_display_result(cpu, outf, "base-freq", "enable",
814 isst_display_result(cpu, outf, "base-freq", "disable",
819 static void set_pbf_enable(void)
825 "Enable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
829 isst_ctdp_display_information_start(outf);
831 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
834 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
836 isst_ctdp_display_information_end(outf);
839 static void set_pbf_disable(void)
845 "Disable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
849 isst_ctdp_display_information_start(outf);
851 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
854 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
856 isst_ctdp_display_information_end(outf);
859 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
860 void *arg3, void *arg4)
862 struct isst_fact_info fact_info;
865 ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
867 perror("isst_get_fact_bucket_info");
869 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
870 fact_avx, &fact_info);
873 static void dump_fact_config(void)
877 "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
879 "\tArguments: -l|--level : Specify tdp level\n");
881 "\tArguments: -b|--bucket : Bucket index to dump\n");
883 "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
887 if (tdp_level == 0xff) {
888 fprintf(outf, "Invalid command: specify tdp_level\n");
892 isst_ctdp_display_information_start(outf);
894 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
895 NULL, NULL, NULL, NULL);
897 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
899 isst_ctdp_display_information_end(outf);
902 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
906 int status = *(int *)arg4;
908 ret = isst_set_pbf_fact_status(cpu, 0, status);
910 perror("isst_set_fact");
913 struct isst_pkg_ctdp pkg_dev;
915 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
917 isst_display_result(cpu, outf, "turbo-freq",
921 ret = isst_set_trl(cpu, fact_trl);
922 isst_display_result(cpu, outf, "turbo-freq", "enable",
925 /* Since we modified TRL during Fact enable, restore it */
926 isst_set_trl_from_current_tdp(cpu, fact_trl);
927 isst_display_result(cpu, outf, "turbo-freq", "disable",
933 static void set_fact_enable(void)
939 "Enable Intel Speed Select Technology Turbo frequency feature\n");
941 "Optional: -t|--trl : Specify turbo ratio limit\n");
945 isst_ctdp_display_information_start(outf);
947 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
950 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
952 isst_ctdp_display_information_end(outf);
955 static void set_fact_disable(void)
961 "Disable Intel Speed Select Technology turbo frequency feature\n");
963 "Optional: -t|--trl : Specify turbo ratio limit\n");
967 isst_ctdp_display_information_start(outf);
969 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
972 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
974 isst_ctdp_display_information_end(outf);
977 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
981 int status = *(int *)arg4;
983 ret = isst_pm_qos_config(cpu, status, clos_priority_type);
985 perror("isst_pm_qos_config");
988 isst_display_result(cpu, outf, "core-power", "enable",
991 isst_display_result(cpu, outf, "core-power", "disable",
996 static void set_clos_enable(void)
1001 fprintf(stderr, "Enable core-power for a package/die\n");
1003 "\tClos Enable: Specify priority type with [--priority|-p]\n");
1004 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1008 if (cpufreq_sysfs_present()) {
1010 "cpufreq subsystem and core-power enable will interfere with each other!\n");
1013 isst_ctdp_display_information_start(outf);
1014 if (max_target_cpus)
1015 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1016 NULL, NULL, &status);
1018 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1019 NULL, NULL, &status);
1020 isst_ctdp_display_information_end(outf);
1023 static void set_clos_disable(void)
1029 "Disable core-power: [No command arguments are required]\n");
1033 isst_ctdp_display_information_start(outf);
1034 if (max_target_cpus)
1035 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1036 NULL, NULL, &status);
1038 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1039 NULL, NULL, &status);
1040 isst_ctdp_display_information_end(outf);
1043 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1044 void *arg3, void *arg4)
1046 struct isst_clos_config clos_config;
1049 ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1051 perror("isst_pm_get_clos");
1053 isst_clos_display_information(cpu, outf, current_clos,
1057 static void dump_clos_config(void)
1061 "Print Intel Speed Select Technology core power configuration\n");
1063 "\tArguments: [-c | --clos]: Specify clos id\n");
1066 if (current_clos < 0 || current_clos > 3) {
1067 fprintf(stderr, "Invalid clos id\n");
1071 isst_ctdp_display_information_start(outf);
1072 if (max_target_cpus)
1073 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
1074 NULL, NULL, NULL, NULL);
1076 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
1078 isst_ctdp_display_information_end(outf);
1081 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1084 struct isst_clos_config clos_config;
1087 clos_config.pkg_id = get_physical_package_id(cpu);
1088 clos_config.die_id = get_physical_die_id(cpu);
1090 clos_config.epp = clos_epp;
1091 clos_config.clos_prop_prio = clos_prop_prio;
1092 clos_config.clos_min = clos_min;
1093 clos_config.clos_max = clos_max;
1094 clos_config.clos_desired = clos_desired;
1095 ret = isst_set_clos(cpu, current_clos, &clos_config);
1097 perror("isst_set_clos");
1099 isst_display_result(cpu, outf, "core-power", "config", ret);
1102 static void set_clos_config(void)
1106 "Set core-power configuration for one of the four clos ids\n");
1108 "\tSpecify targeted clos id with [--clos|-c]\n");
1109 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
1111 "\tSpecify clos Proportional Priority [--weight|-w]\n");
1112 fprintf(stderr, "\tSpecify clos min with [--min|-n]\n");
1113 fprintf(stderr, "\tSpecify clos max with [--max|-m]\n");
1114 fprintf(stderr, "\tSpecify clos desired with [--desired|-d]\n");
1118 if (current_clos < 0 || current_clos > 3) {
1119 fprintf(stderr, "Invalid clos id\n");
1122 if (clos_epp < 0 || clos_epp > 0x0F) {
1123 fprintf(stderr, "clos epp is not specified, default: 0\n");
1126 if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
1128 "clos frequency weight is not specified, default: 0\n");
1132 fprintf(stderr, "clos min is not specified, default: 0\n");
1136 fprintf(stderr, "clos max is not specified, default: 0xff\n");
1139 if (clos_desired < 0) {
1140 fprintf(stderr, "clos desired is not specified, default: 0\n");
1141 clos_desired = 0x00;
1144 isst_ctdp_display_information_start(outf);
1145 if (max_target_cpus)
1146 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
1149 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
1151 isst_ctdp_display_information_end(outf);
1154 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1159 ret = isst_clos_associate(cpu, current_clos);
1161 perror("isst_clos_associate");
1163 isst_display_result(cpu, outf, "core-power", "assoc", ret);
1166 static void set_clos_assoc(void)
1169 fprintf(stderr, "Associate a clos id to a CPU\n");
1171 "\tSpecify targeted clos id with [--clos|-c]\n");
1175 if (current_clos < 0 || current_clos > 3) {
1176 fprintf(stderr, "Invalid clos id\n");
1179 if (max_target_cpus)
1180 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
1184 "Invalid target cpu. Specify with [-c|--cpu]\n");
1188 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1193 ret = isst_clos_get_assoc_status(cpu, &clos);
1195 perror("isst_clos_get_assoc_status");
1197 isst_display_result(cpu, outf, "core-power", "get-assoc", clos);
1200 static void get_clos_assoc(void)
1203 fprintf(stderr, "Get associate clos id to a CPU\n");
1204 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
1207 if (max_target_cpus)
1208 for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
1212 "Invalid target cpu. Specify with [-c|--cpu]\n");
1216 static struct process_cmd_struct isst_cmds[] = {
1217 { "perf-profile", "get-lock-status", get_tdp_locked },
1218 { "perf-profile", "get-config-levels", get_tdp_levels },
1219 { "perf-profile", "get-config-version", get_tdp_version },
1220 { "perf-profile", "get-config-enabled", get_tdp_enabled },
1221 { "perf-profile", "get-config-current-level", get_tdp_current_level },
1222 { "perf-profile", "set-config-level", set_tdp_level },
1223 { "perf-profile", "info", dump_isst_config },
1224 { "base-freq", "info", dump_pbf_config },
1225 { "base-freq", "enable", set_pbf_enable },
1226 { "base-freq", "disable", set_pbf_disable },
1227 { "turbo-freq", "info", dump_fact_config },
1228 { "turbo-freq", "enable", set_fact_enable },
1229 { "turbo-freq", "disable", set_fact_disable },
1230 { "core-power", "info", dump_clos_config },
1231 { "core-power", "enable", set_clos_enable },
1232 { "core-power", "disable", set_clos_disable },
1233 { "core-power", "config", set_clos_config },
1234 { "core-power", "assoc", set_clos_assoc },
1235 { "core-power", "get-assoc", get_clos_assoc },
1236 { NULL, NULL, NULL }
1240 * parse cpuset with following syntax
1241 * 1,2,4..6,8-10 and set bits in cpu_subset
1243 void parse_cpu_command(char *optarg)
1245 unsigned int start, end;
1250 while (next && *next) {
1251 if (*next == '-') /* no negative cpu numbers */
1254 start = strtoul(next, &next, 10);
1256 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1257 target_cpus[max_target_cpus++] = start;
1268 next += 1; /* start range */
1269 } else if (*next == '.') {
1272 next += 1; /* start range */
1277 end = strtoul(next, &next, 10);
1281 while (++start <= end) {
1282 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1283 target_cpus[max_target_cpus++] = start;
1288 else if (*next != '\0')
1296 for (i = 0; i < max_target_cpus; ++i)
1297 printf("cpu [%d] in arg\n", target_cpus[i]);
1303 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
1307 static void parse_cmd_args(int argc, int start, char **argv)
1312 static struct option long_options[] = {
1313 { "bucket", required_argument, 0, 'b' },
1314 { "level", required_argument, 0, 'l' },
1315 { "trl-type", required_argument, 0, 'r' },
1316 { "trl", required_argument, 0, 't' },
1317 { "help", no_argument, 0, 'h' },
1318 { "clos", required_argument, 0, 'c' },
1319 { "desired", required_argument, 0, 'd' },
1320 { "epp", required_argument, 0, 'e' },
1321 { "min", required_argument, 0, 'n' },
1322 { "max", required_argument, 0, 'm' },
1323 { "priority", required_argument, 0, 'p' },
1324 { "weight", required_argument, 0, 'w' },
1328 option_index = start;
1331 while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:h",
1332 long_options, &option_index)) != -1) {
1335 fact_bucket = atoi(optarg);
1341 tdp_level = atoi(optarg);
1344 sscanf(optarg, "0x%llx", &fact_trl);
1347 if (!strncmp(optarg, "sse", 3)) {
1349 } else if (!strncmp(optarg, "avx2", 4)) {
1351 } else if (!strncmp(optarg, "avx512", 4)) {
1354 fprintf(outf, "Invalid sse,avx options\n");
1360 current_clos = atoi(optarg);
1361 printf("clos %d\n", current_clos);
1364 clos_desired = atoi(optarg);
1367 clos_epp = atoi(optarg);
1370 clos_min = atoi(optarg);
1373 clos_max = atoi(optarg);
1376 clos_priority_type = atoi(optarg);
1379 clos_prop_prio = atoi(optarg);
1382 printf("no match\n");
1387 static void isst_help(void)
1389 printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
1390 performance profiles per system via static and/or dynamic\n\
1391 adjustment of core count, workload, Tjmax, and\n\
1393 printf("\nCommands : For feature=perf-profile\n");
1395 printf("\tget-lock-status\n");
1396 printf("\tget-config-levels\n");
1397 printf("\tget-config-version\n");
1398 printf("\tget-config-enabled\n");
1399 printf("\tget-config-current-level\n");
1400 printf("\tset-config-level\n");
1403 static void pbf_help(void)
1405 printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
1406 on certain cores (high priority cores) in exchange for lower\n\
1407 base frequency on remaining cores (low priority cores).\n");
1408 printf("\tcommand : info\n");
1409 printf("\tcommand : enable\n");
1410 printf("\tcommand : disable\n");
1413 static void fact_help(void)
1415 printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
1416 limits to cores based on priority.\n");
1417 printf("\nCommand: For feature=turbo-freq\n");
1418 printf("\tcommand : info\n");
1419 printf("\tcommand : enable\n");
1420 printf("\tcommand : disable\n");
1423 static void core_power_help(void)
1425 printf("core-power:\tInterface that allows user to define per core/tile\n\
1427 printf("\nCommands : For feature=core-power\n");
1429 printf("\tenable\n");
1430 printf("\tdisable\n");
1431 printf("\tconfig\n");
1432 printf("\tassoc\n");
1433 printf("\tget-assoc\n");
1436 struct process_cmd_help_struct {
1438 void (*process_fn)(void);
1441 static struct process_cmd_help_struct isst_help_cmds[] = {
1442 { "perf-profile", isst_help },
1443 { "base-freq", pbf_help },
1444 { "turbo-freq", fact_help },
1445 { "core-power", core_power_help },
1449 void process_command(int argc, char **argv)
1451 int i = 0, matched = 0;
1452 char *feature = argv[optind];
1453 char *cmd = argv[optind + 1];
1455 if (!feature || !cmd)
1458 debug_printf("feature name [%s] command [%s]\n", feature, cmd);
1459 if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
1460 while (isst_help_cmds[i].feature) {
1461 if (!strcmp(isst_help_cmds[i].feature, feature)) {
1462 isst_help_cmds[i].process_fn();
1472 while (isst_cmds[i].feature) {
1473 if (!strcmp(isst_cmds[i].feature, feature) &&
1474 !strcmp(isst_cmds[i].command, cmd)) {
1475 parse_cmd_args(argc, optind + 1, argv);
1476 isst_cmds[i].process_fn();
1484 fprintf(stderr, "Invalid command\n");
1487 static void usage(void)
1489 printf("Intel(R) Speed Select Technology\n");
1490 printf("\nUsage:\n");
1491 printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
1492 printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features,\n");
1493 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
1494 printf("\nFor help on each feature, use --h|--help\n");
1495 printf("\tFor example: intel-speed-select perf-profile -h\n");
1497 printf("\nFor additional help on each command for a feature, use --h|--help\n");
1498 printf("\tFor example: intel-speed-select perf-profile get-lock-status -h\n");
1499 printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
1501 printf("\nOPTIONS\n");
1502 printf("\t[-c|--cpu] : logical cpu number\n");
1503 printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
1504 printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
1505 printf("\t[-d|--debug] : Debug mode\n");
1506 printf("\t[-h|--help] : Print help\n");
1507 printf("\t[-i|--info] : Print platform information\n");
1508 printf("\t[-o|--out] : Output file\n");
1509 printf("\t\t\tDefault : stderr\n");
1510 printf("\t[-f|--format] : output format [json|text]. Default: text\n");
1511 printf("\t[-v|--version] : Print version\n");
1513 printf("\nResult format\n");
1514 printf("\tResult display uses a common format for each command:\n");
1515 printf("\tResults are formatted in text/JSON with\n");
1516 printf("\t\tPackage, Die, CPU, and command specific results.\n");
1517 printf("\t\t\tFor Set commands, status is 0 for success and rest for failures\n");
1521 static void print_version(void)
1523 fprintf(outf, "Version %s\n", version_str);
1524 fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
1528 static void cmdline(int argc, char **argv)
1531 int option_index = 0;
1533 static struct option long_options[] = {
1534 { "cpu", required_argument, 0, 'c' },
1535 { "debug", no_argument, 0, 'd' },
1536 { "format", required_argument, 0, 'f' },
1537 { "help", no_argument, 0, 'h' },
1538 { "info", no_argument, 0, 'i' },
1539 { "out", required_argument, 0, 'o' },
1540 { "version", no_argument, 0, 'v' },
1545 while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
1546 &option_index)) != -1) {
1549 parse_cpu_command(optarg);
1553 printf("Debug Mode ON\n");
1556 if (!strncmp(optarg, "json", 4))
1557 out_format_json = 1;
1563 isst_print_platform_information();
1568 outf = fopen_or_exit(optarg, "w");
1578 if (geteuid() != 0) {
1579 fprintf(stderr, "Must run as root\n");
1583 if (optind > (argc - 2)) {
1584 fprintf(stderr, "Feature name and|or command not specified\n");
1588 printf("Intel(R) Speed Select Technology\n");
1589 printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
1591 set_cpu_present_cpu_mask();
1592 set_cpu_target_cpu_mask();
1593 isst_fill_platform_info();
1594 if (isst_platform_info.api_version > supported_api_ver) {
1595 printf("Incompatible API versions; Upgrade of tool is required\n");
1599 process_command(argc, argv);
1602 int main(int argc, char **argv)
1605 cmdline(argc, argv);