tools/power/x86/intel-speed-select: Turbo-freq feature auto mode
[linux-2.6-microblaze.git] / tools / power / x86 / intel-speed-select / isst-config.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Speed Select -- Enumerate and control features
4  * Copyright (c) 2019 Intel Corporation.
5  */
6
7 #include <linux/isst_if.h>
8
9 #include "isst.h"
10
11 struct process_cmd_struct {
12         char *feature;
13         char *command;
14         void (*process_fn)(void);
15 };
16
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;
22 static FILE *outf;
23
24 static int cpu_model;
25
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];
29
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;
40 static int cmd_help;
41 static int force_online_offline;
42 static int auto_mode;
43
44 /* clos related */
45 static int current_clos = -1;
46 static int clos_epp = -1;
47 static int clos_prop_prio = -1;
48 static int clos_min = -1;
49 static int clos_max = -1;
50 static int clos_desired = -1;
51 static int clos_priority_type;
52
53 struct _cpu_map {
54         unsigned short core_id;
55         unsigned short pkg_id;
56         unsigned short die_id;
57         unsigned short punit_cpu;
58         unsigned short punit_cpu_core;
59 };
60 struct _cpu_map *cpu_map;
61
62 void debug_printf(const char *format, ...)
63 {
64         va_list args;
65
66         va_start(args, format);
67
68         if (debug_flag)
69                 vprintf(format, args);
70
71         va_end(args);
72 }
73
74 static void update_cpu_model(void)
75 {
76         unsigned int ebx, ecx, edx;
77         unsigned int fms, family;
78
79         __cpuid(1, fms, ebx, ecx, edx);
80         family = (fms >> 8) & 0xf;
81         cpu_model = (fms >> 4) & 0xf;
82         if (family == 6 || family == 0xf)
83                 cpu_model += ((fms >> 16) & 0xf) << 4;
84 }
85
86 /* Open a file, and exit on failure */
87 static FILE *fopen_or_exit(const char *path, const char *mode)
88 {
89         FILE *filep = fopen(path, mode);
90
91         if (!filep)
92                 err(1, "%s: open failed", path);
93
94         return filep;
95 }
96
97 /* Parse a file containing a single int */
98 static int parse_int_file(int fatal, const char *fmt, ...)
99 {
100         va_list args;
101         char path[PATH_MAX];
102         FILE *filep;
103         int value;
104
105         va_start(args, fmt);
106         vsnprintf(path, sizeof(path), fmt, args);
107         va_end(args);
108         if (fatal) {
109                 filep = fopen_or_exit(path, "r");
110         } else {
111                 filep = fopen(path, "r");
112                 if (!filep)
113                         return -1;
114         }
115         if (fscanf(filep, "%d", &value) != 1)
116                 err(1, "%s: failed to parse number from file", path);
117         fclose(filep);
118
119         return value;
120 }
121
122 int cpufreq_sysfs_present(void)
123 {
124         DIR *dir;
125
126         dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
127         if (dir) {
128                 closedir(dir);
129                 return 1;
130         }
131
132         return 0;
133 }
134
135 int out_format_is_json(void)
136 {
137         return out_format_json;
138 }
139
140 int get_physical_package_id(int cpu)
141 {
142         return parse_int_file(
143                 0, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
144                 cpu);
145 }
146
147 int get_physical_core_id(int cpu)
148 {
149         return parse_int_file(
150                 0, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
151 }
152
153 int get_physical_die_id(int cpu)
154 {
155         int ret;
156
157         ret = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/topology/die_id",
158                              cpu);
159         if (ret < 0)
160                 ret = 0;
161
162         return ret;
163 }
164
165 int get_topo_max_cpus(void)
166 {
167         return topo_max_cpus;
168 }
169
170 static void set_cpu_online_offline(int cpu, int state)
171 {
172         char buffer[128];
173         int fd, ret;
174
175         snprintf(buffer, sizeof(buffer),
176                  "/sys/devices/system/cpu/cpu%d/online", cpu);
177
178         fd = open(buffer, O_WRONLY);
179         if (fd < 0)
180                 err(-1, "%s open failed", buffer);
181
182         if (state)
183                 ret = write(fd, "1\n", 2);
184         else
185                 ret = write(fd, "0\n", 2);
186
187         if (ret == -1)
188                 perror("Online/Offline: Operation failed\n");
189
190         close(fd);
191 }
192
193 #define MAX_PACKAGE_COUNT 8
194 #define MAX_DIE_PER_PACKAGE 2
195 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
196                                                             void *, void *),
197                                            void *arg1, void *arg2, void *arg3,
198                                            void *arg4)
199 {
200         int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
201         int pkg_index = 0, i;
202
203         memset(max_packages, 0xff, sizeof(max_packages));
204         for (i = 0; i < topo_max_cpus; ++i) {
205                 int j, online, pkg_id, die_id = 0, skip = 0;
206
207                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
208                         continue;
209                 if (i)
210                         online = parse_int_file(
211                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
212                 else
213                         online =
214                                 1; /* online entry for CPU 0 needs some special configs */
215
216                 die_id = get_physical_die_id(i);
217                 if (die_id < 0)
218                         die_id = 0;
219                 pkg_id = get_physical_package_id(i);
220                 /* Create an unique id for package, die combination to store */
221                 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
222
223                 for (j = 0; j < pkg_index; ++j) {
224                         if (max_packages[j] == pkg_id) {
225                                 skip = 1;
226                                 break;
227                         }
228                 }
229
230                 if (!skip && online && callback) {
231                         callback(i, arg1, arg2, arg3, arg4);
232                         max_packages[pkg_index++] = pkg_id;
233                 }
234         }
235 }
236
237 static void for_each_online_target_cpu_in_set(
238         void (*callback)(int, void *, void *, void *, void *), void *arg1,
239         void *arg2, void *arg3, void *arg4)
240 {
241         int i;
242
243         for (i = 0; i < topo_max_cpus; ++i) {
244                 int online;
245
246                 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
247                         continue;
248                 if (i)
249                         online = parse_int_file(
250                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
251                 else
252                         online =
253                                 1; /* online entry for CPU 0 needs some special configs */
254
255                 if (online && callback)
256                         callback(i, arg1, arg2, arg3, arg4);
257         }
258 }
259
260 #define BITMASK_SIZE 32
261 static void set_max_cpu_num(void)
262 {
263         FILE *filep;
264         unsigned long dummy;
265
266         topo_max_cpus = 0;
267         filep = fopen_or_exit(
268                 "/sys/devices/system/cpu/cpu0/topology/thread_siblings", "r");
269         while (fscanf(filep, "%lx,", &dummy) == 1)
270                 topo_max_cpus += BITMASK_SIZE;
271         fclose(filep);
272         topo_max_cpus--; /* 0 based */
273
274         debug_printf("max cpus %d\n", topo_max_cpus);
275 }
276
277 size_t alloc_cpu_set(cpu_set_t **cpu_set)
278 {
279         cpu_set_t *_cpu_set;
280         size_t size;
281
282         _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
283         if (_cpu_set == NULL)
284                 err(3, "CPU_ALLOC");
285         size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
286         CPU_ZERO_S(size, _cpu_set);
287
288         *cpu_set = _cpu_set;
289         return size;
290 }
291
292 void free_cpu_set(cpu_set_t *cpu_set)
293 {
294         CPU_FREE(cpu_set);
295 }
296
297 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
298 static void set_cpu_present_cpu_mask(void)
299 {
300         size_t size;
301         DIR *dir;
302         int i;
303
304         size = alloc_cpu_set(&present_cpumask);
305         present_cpumask_size = size;
306         for (i = 0; i < topo_max_cpus; ++i) {
307                 char buffer[256];
308
309                 snprintf(buffer, sizeof(buffer),
310                          "/sys/devices/system/cpu/cpu%d", i);
311                 dir = opendir(buffer);
312                 if (dir) {
313                         int pkg_id, die_id;
314
315                         CPU_SET_S(i, size, present_cpumask);
316                         die_id = get_physical_die_id(i);
317                         if (die_id < 0)
318                                 die_id = 0;
319
320                         pkg_id = get_physical_package_id(i);
321                         if (pkg_id < MAX_PACKAGE_COUNT &&
322                             die_id < MAX_DIE_PER_PACKAGE)
323                                 cpu_cnt[pkg_id][die_id]++;
324                 }
325                 closedir(dir);
326         }
327 }
328
329 int get_cpu_count(int pkg_id, int die_id)
330 {
331         if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
332                 return cpu_cnt[pkg_id][die_id];
333
334         return 0;
335 }
336
337 static void set_cpu_target_cpu_mask(void)
338 {
339         size_t size;
340         int i;
341
342         size = alloc_cpu_set(&target_cpumask);
343         target_cpumask_size = size;
344         for (i = 0; i < max_target_cpus; ++i) {
345                 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
346                                  present_cpumask))
347                         continue;
348
349                 CPU_SET_S(target_cpus[i], size, target_cpumask);
350         }
351 }
352
353 static void create_cpu_map(void)
354 {
355         const char *pathname = "/dev/isst_interface";
356         int i, fd = 0;
357         struct isst_if_cpu_maps map;
358
359         cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
360         if (!cpu_map)
361                 err(3, "cpumap");
362
363         fd = open(pathname, O_RDWR);
364         if (fd < 0)
365                 err(-1, "%s open failed", pathname);
366
367         for (i = 0; i < topo_max_cpus; ++i) {
368                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
369                         continue;
370
371                 map.cmd_count = 1;
372                 map.cpu_map[0].logical_cpu = i;
373
374                 debug_printf(" map logical_cpu:%d\n",
375                              map.cpu_map[0].logical_cpu);
376                 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
377                         perror("ISST_IF_GET_PHY_ID");
378                         fprintf(outf, "Error: map logical_cpu:%d\n",
379                                 map.cpu_map[0].logical_cpu);
380                         continue;
381                 }
382                 cpu_map[i].core_id = get_physical_core_id(i);
383                 cpu_map[i].pkg_id = get_physical_package_id(i);
384                 cpu_map[i].die_id = get_physical_die_id(i);
385                 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
386                 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
387                                              1); // shift to get core id
388
389                 debug_printf(
390                         "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
391                         i, cpu_map[i].core_id, cpu_map[i].die_id,
392                         cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
393                         cpu_map[i].punit_cpu_core);
394         }
395
396         if (fd)
397                 close(fd);
398 }
399
400 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
401 {
402         int i;
403
404         for (i = 0; i < topo_max_cpus; ++i) {
405                 if (cpu_map[i].pkg_id == pkg_id &&
406                     cpu_map[i].die_id == die_id &&
407                     cpu_map[i].punit_cpu_core == punit_core_id)
408                         return i;
409         }
410
411         return -EINVAL;
412 }
413
414 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
415                                       size_t core_cpumask_size,
416                                       cpu_set_t *core_cpumask, int *cpu_cnt)
417 {
418         int i, cnt = 0;
419         int die_id, pkg_id;
420
421         *cpu_cnt = 0;
422         die_id = get_physical_die_id(cpu);
423         pkg_id = get_physical_package_id(cpu);
424
425         for (i = 0; i < 64; ++i) {
426                 if (core_mask & BIT(i)) {
427                         int j;
428
429                         for (j = 0; j < topo_max_cpus; ++j) {
430                                 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
431                                         continue;
432
433                                 if (cpu_map[j].pkg_id == pkg_id &&
434                                     cpu_map[j].die_id == die_id &&
435                                     cpu_map[j].punit_cpu_core == i) {
436                                         CPU_SET_S(j, core_cpumask_size,
437                                                   core_cpumask);
438                                         ++cnt;
439                                 }
440                         }
441                 }
442         }
443
444         *cpu_cnt = cnt;
445 }
446
447 int find_phy_core_num(int logical_cpu)
448 {
449         if (logical_cpu < topo_max_cpus)
450                 return cpu_map[logical_cpu].punit_cpu_core;
451
452         return -EINVAL;
453 }
454
455 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
456                                   unsigned int *value)
457 {
458         struct isst_if_io_regs io_regs;
459         const char *pathname = "/dev/isst_interface";
460         int cmd;
461         int fd;
462
463         debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
464
465         fd = open(pathname, O_RDWR);
466         if (fd < 0)
467                 err(-1, "%s open failed", pathname);
468
469         io_regs.req_count = 1;
470         io_regs.io_reg[0].logical_cpu = cpu;
471         io_regs.io_reg[0].reg = reg;
472         cmd = ISST_IF_IO_CMD;
473         if (write) {
474                 io_regs.io_reg[0].read_write = 1;
475                 io_regs.io_reg[0].value = *value;
476         } else {
477                 io_regs.io_reg[0].read_write = 0;
478         }
479
480         if (ioctl(fd, cmd, &io_regs) == -1) {
481                 perror("ISST_IF_IO_CMD");
482                 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
483                         cpu, reg, write);
484         } else {
485                 if (!write)
486                         *value = io_regs.io_reg[0].value;
487
488                 debug_printf(
489                         "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
490                         cpu, reg, write, *value);
491         }
492
493         close(fd);
494
495         return 0;
496 }
497
498 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
499                            unsigned char sub_command, unsigned int parameter,
500                            unsigned int req_data, unsigned int *resp)
501 {
502         const char *pathname = "/dev/isst_interface";
503         int fd;
504         struct isst_if_mbox_cmds mbox_cmds = { 0 };
505
506         debug_printf(
507                 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
508                 cpu, command, sub_command, parameter, req_data);
509
510         if (isst_platform_info.mmio_supported && command == CONFIG_CLOS) {
511                 unsigned int value;
512                 int write = 0;
513                 int clos_id, core_id, ret = 0;
514
515                 debug_printf("CPU %d\n", cpu);
516
517                 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
518                         value = req_data;
519                         write = 1;
520                 }
521
522                 switch (sub_command) {
523                 case CLOS_PQR_ASSOC:
524                         core_id = parameter & 0xff;
525                         ret = isst_send_mmio_command(
526                                 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
527                                 &value);
528                         if (!ret && !write)
529                                 *resp = value;
530                         break;
531                 case CLOS_PM_CLOS:
532                         clos_id = parameter & 0x03;
533                         ret = isst_send_mmio_command(
534                                 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
535                                 &value);
536                         if (!ret && !write)
537                                 *resp = value;
538                         break;
539                 case CLOS_PM_QOS_CONFIG:
540                         ret = isst_send_mmio_command(cpu, PM_QOS_CONFIG_OFFSET,
541                                                      write, &value);
542                         if (!ret && !write)
543                                 *resp = value;
544                         break;
545                 case CLOS_STATUS:
546                         break;
547                 default:
548                         break;
549                 }
550                 return ret;
551         }
552
553         mbox_cmds.cmd_count = 1;
554         mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
555         mbox_cmds.mbox_cmd[0].command = command;
556         mbox_cmds.mbox_cmd[0].sub_command = sub_command;
557         mbox_cmds.mbox_cmd[0].parameter = parameter;
558         mbox_cmds.mbox_cmd[0].req_data = req_data;
559
560         fd = open(pathname, O_RDWR);
561         if (fd < 0)
562                 err(-1, "%s open failed", pathname);
563
564         if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
565                 perror("ISST_IF_MBOX_COMMAND");
566                 fprintf(outf,
567                         "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
568                         cpu, command, sub_command, parameter, req_data);
569         } else {
570                 *resp = mbox_cmds.mbox_cmd[0].resp_data;
571                 debug_printf(
572                         "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
573                         cpu, command, sub_command, parameter, req_data, *resp);
574         }
575
576         close(fd);
577
578         return 0;
579 }
580
581 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
582                           unsigned long long *req_resp)
583 {
584         struct isst_if_msr_cmds msr_cmds;
585         const char *pathname = "/dev/isst_interface";
586         int fd;
587
588         fd = open(pathname, O_RDWR);
589         if (fd < 0)
590                 err(-1, "%s open failed", pathname);
591
592         msr_cmds.cmd_count = 1;
593         msr_cmds.msr_cmd[0].logical_cpu = cpu;
594         msr_cmds.msr_cmd[0].msr = msr;
595         msr_cmds.msr_cmd[0].read_write = write;
596         if (write)
597                 msr_cmds.msr_cmd[0].data = *req_resp;
598
599         if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
600                 perror("ISST_IF_MSR_COMMAD");
601                 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
602                         cpu, msr, write);
603         } else {
604                 if (!write)
605                         *req_resp = msr_cmds.msr_cmd[0].data;
606
607                 debug_printf(
608                         "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
609                         cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
610         }
611
612         close(fd);
613
614         return 0;
615 }
616
617 static int isst_fill_platform_info(void)
618 {
619         const char *pathname = "/dev/isst_interface";
620         int fd;
621
622         fd = open(pathname, O_RDWR);
623         if (fd < 0)
624                 err(-1, "%s open failed", pathname);
625
626         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
627                 perror("ISST_IF_GET_PLATFORM_INFO");
628                 close(fd);
629                 return -1;
630         }
631
632         close(fd);
633
634         if (isst_platform_info.api_version > supported_api_ver) {
635                 printf("Incompatible API versions; Upgrade of tool is required\n");
636                 return -1;
637         }
638         return 0;
639 }
640
641 static void isst_print_platform_information(void)
642 {
643         struct isst_if_platform_info platform_info;
644         const char *pathname = "/dev/isst_interface";
645         int fd;
646
647         fd = open(pathname, O_RDWR);
648         if (fd < 0)
649                 err(-1, "%s open failed", pathname);
650
651         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
652                 perror("ISST_IF_GET_PLATFORM_INFO");
653         } else {
654                 fprintf(outf, "Platform: API version : %d\n",
655                         platform_info.api_version);
656                 fprintf(outf, "Platform: Driver version : %d\n",
657                         platform_info.driver_version);
658                 fprintf(outf, "Platform: mbox supported : %d\n",
659                         platform_info.mbox_supported);
660                 fprintf(outf, "Platform: mmio supported : %d\n",
661                         platform_info.mmio_supported);
662         }
663
664         close(fd);
665
666         exit(0);
667 }
668
669 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
670                                  void *arg4)
671 {
672         int (*fn_ptr)(int cpu, void *arg);
673         int ret;
674
675         fn_ptr = arg1;
676         ret = fn_ptr(cpu, arg2);
677         if (ret)
678                 perror("get_tdp_*");
679         else
680                 isst_ctdp_display_core_info(cpu, outf, arg3,
681                                             *(unsigned int *)arg4);
682 }
683
684 #define _get_tdp_level(desc, suffix, object, help)                                \
685         static void get_tdp_##object(void)                                        \
686         {                                                                         \
687                 struct isst_pkg_ctdp ctdp;                                        \
688 \
689                 if (cmd_help) {                                                   \
690                         fprintf(stderr,                                           \
691                                 "Print %s [No command arguments are required]\n", \
692                                 help);                                            \
693                         exit(0);                                                  \
694                 }                                                                 \
695                 isst_ctdp_display_information_start(outf);                        \
696                 if (max_target_cpus)                                              \
697                         for_each_online_target_cpu_in_set(                        \
698                                 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
699                                 &ctdp, desc, &ctdp.object);                       \
700                 else                                                              \
701                         for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
702                                                        isst_get_ctdp_##suffix,    \
703                                                        &ctdp, desc,               \
704                                                        &ctdp.object);             \
705                 isst_ctdp_display_information_end(outf);                          \
706         }
707
708 _get_tdp_level("get-config-levels", levels, levels, "TDP levels");
709 _get_tdp_level("get-config-version", levels, version, "TDP version");
710 _get_tdp_level("get-config-enabled", levels, enabled, "TDP enable status");
711 _get_tdp_level("get-config-current_level", levels, current_level,
712                "Current TDP Level");
713 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status");
714
715 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
716                                      void *arg3, void *arg4)
717 {
718         struct isst_pkg_ctdp pkg_dev;
719         int ret;
720
721         memset(&pkg_dev, 0, sizeof(pkg_dev));
722         ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
723         if (ret) {
724                 perror("isst_get_process_ctdp");
725         } else {
726                 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
727                 isst_get_process_ctdp_complete(cpu, &pkg_dev);
728         }
729 }
730
731 static void dump_isst_config(void)
732 {
733         if (cmd_help) {
734                 fprintf(stderr,
735                         "Print Intel(R) Speed Select Technology Performance profile configuration\n");
736                 fprintf(stderr,
737                         "including base frequency and turbo frequency configurations\n");
738                 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
739                 fprintf(stderr,
740                         "\tIf no arguments, dump information for all TDP levels\n");
741                 exit(0);
742         }
743
744         isst_ctdp_display_information_start(outf);
745
746         if (max_target_cpus)
747                 for_each_online_target_cpu_in_set(dump_isst_config_for_cpu,
748                                                   NULL, NULL, NULL, NULL);
749         else
750                 for_each_online_package_in_set(dump_isst_config_for_cpu, NULL,
751                                                NULL, NULL, NULL);
752
753         isst_ctdp_display_information_end(outf);
754 }
755
756 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
757                                   void *arg4)
758 {
759         int ret;
760
761         ret = isst_set_tdp_level(cpu, tdp_level);
762         if (ret)
763                 perror("set_tdp_level_for_cpu");
764         else {
765                 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
766                                     ret);
767                 if (force_online_offline) {
768                         struct isst_pkg_ctdp_level_info ctdp_level;
769                         int pkg_id = get_physical_package_id(cpu);
770                         int die_id = get_physical_die_id(cpu);
771
772                         fprintf(stderr, "Option is set to online/offline\n");
773                         ctdp_level.core_cpumask_size =
774                                 alloc_cpu_set(&ctdp_level.core_cpumask);
775                         isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
776                         if (ctdp_level.cpu_count) {
777                                 int i, max_cpus = get_topo_max_cpus();
778                                 for (i = 0; i < max_cpus; ++i) {
779                                         if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
780                                                 continue;
781                                         if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
782                                                 fprintf(stderr, "online cpu %d\n", i);
783                                                 set_cpu_online_offline(i, 1);
784                                         } else {
785                                                 fprintf(stderr, "offline cpu %d\n", i);
786                                                 set_cpu_online_offline(i, 0);
787                                         }
788                                 }
789                         }
790                 }
791         }
792 }
793
794 static void set_tdp_level(void)
795 {
796         if (cmd_help) {
797                 fprintf(stderr, "Set Config TDP level\n");
798                 fprintf(stderr,
799                         "\t Arguments: -l|--level : Specify tdp level\n");
800                 fprintf(stderr,
801                         "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
802                 exit(0);
803         }
804
805         if (tdp_level == 0xff) {
806                 fprintf(outf, "Invalid command: specify tdp_level\n");
807                 exit(1);
808         }
809         isst_ctdp_display_information_start(outf);
810         if (max_target_cpus)
811                 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
812                                                   NULL, NULL, NULL);
813         else
814                 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
815                                                NULL, NULL, NULL);
816         isst_ctdp_display_information_end(outf);
817 }
818
819 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
820                                     void *arg4)
821 {
822         struct isst_pbf_info pbf_info;
823         int ret;
824
825         ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
826         if (ret) {
827                 perror("isst_get_pbf_info");
828         } else {
829                 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
830                 isst_get_pbf_info_complete(&pbf_info);
831         }
832 }
833
834 static void dump_pbf_config(void)
835 {
836         if (cmd_help) {
837                 fprintf(stderr,
838                         "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
839                 fprintf(stderr,
840                         "\tArguments: -l|--level : Specify tdp level\n");
841                 exit(0);
842         }
843
844         if (tdp_level == 0xff) {
845                 fprintf(outf, "Invalid command: specify tdp_level\n");
846                 exit(1);
847         }
848
849         isst_ctdp_display_information_start(outf);
850         if (max_target_cpus)
851                 for_each_online_target_cpu_in_set(dump_pbf_config_for_cpu, NULL,
852                                                   NULL, NULL, NULL);
853         else
854                 for_each_online_package_in_set(dump_pbf_config_for_cpu, NULL,
855                                                NULL, NULL, NULL);
856         isst_ctdp_display_information_end(outf);
857 }
858
859 static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
860 {
861         struct isst_clos_config clos_config;
862         int ret;
863
864         ret = isst_pm_get_clos(cpu, clos, &clos_config);
865         if (ret) {
866                 perror("isst_pm_get_clos");
867                 return ret;
868         }
869         clos_config.clos_min = min;
870         clos_config.clos_max = max;
871         clos_config.epp = epp;
872         clos_config.clos_prop_prio = wt;
873         ret = isst_set_clos(cpu, clos, &clos_config);
874         if (ret) {
875                 perror("isst_pm_set_clos");
876                 return ret;
877         }
878
879         return 0;
880 }
881
882 static int set_cpufreq_cpuinfo_scaling_min(int cpu, int max)
883 {
884         char buffer[128], min_freq[16];
885         int fd, ret, len;
886
887         if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
888                 return -1;
889
890         if (max)
891                 snprintf(buffer, sizeof(buffer),
892                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
893         else
894                 snprintf(buffer, sizeof(buffer),
895                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
896
897         fd = open(buffer, O_RDONLY);
898         if (fd < 0)
899                 return fd;
900
901         len = read(fd, min_freq, sizeof(min_freq));
902         close(fd);
903
904         if (len < 0)
905                 return len;
906
907         snprintf(buffer, sizeof(buffer),
908                  "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
909
910         fd = open(buffer, O_WRONLY);
911         if (fd < 0)
912                 return fd;
913
914         len = strlen(min_freq);
915         ret = write(fd, min_freq, len);
916         if (ret == -1) {
917                 close(fd);
918                 return ret;
919         }
920         close(fd);
921
922         return 0;
923 }
924
925 static void set_scaling_min_to_cpuinfo_max(int cpu)
926 {
927         int i, pkg_id, die_id;
928
929         pkg_id = get_physical_package_id(cpu);
930         die_id = get_physical_die_id(cpu);
931         for (i = 0; i < get_topo_max_cpus(); ++i) {
932                 if (pkg_id != get_physical_package_id(i) ||
933                     die_id != get_physical_die_id(i))
934                         continue;
935
936                 set_cpufreq_cpuinfo_scaling_min(i, 1);
937         }
938 }
939
940 static void set_scaling_min_to_cpuinfo_min(int cpu)
941 {
942         int i, pkg_id, die_id;
943
944         pkg_id = get_physical_package_id(cpu);
945         die_id = get_physical_die_id(cpu);
946         for (i = 0; i < get_topo_max_cpus(); ++i) {
947                 if (pkg_id != get_physical_package_id(i) ||
948                     die_id != get_physical_die_id(i))
949                         continue;
950
951                 set_cpufreq_cpuinfo_scaling_min(i, 0);
952         }
953 }
954
955 static int set_core_priority_and_min(int cpu, int mask_size,
956                                      cpu_set_t *cpu_mask, int min_high,
957                                      int min_low)
958 {
959         int pkg_id, die_id, ret, i;
960
961         if (!CPU_COUNT_S(mask_size, cpu_mask))
962                 return -1;
963
964         ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
965         if (ret)
966                 return ret;
967
968         ret = set_clos_param(cpu, 1, 15, 0, min_low, 0xff);
969         if (ret)
970                 return ret;
971
972         ret = set_clos_param(cpu, 2, 15, 0, min_low, 0xff);
973         if (ret)
974                 return ret;
975
976         ret = set_clos_param(cpu, 3, 15, 0, min_low, 0xff);
977         if (ret)
978                 return ret;
979
980         pkg_id = get_physical_package_id(cpu);
981         die_id = get_physical_die_id(cpu);
982         for (i = 0; i < get_topo_max_cpus(); ++i) {
983                 int clos;
984
985                 if (pkg_id != get_physical_package_id(i) ||
986                     die_id != get_physical_die_id(i))
987                         continue;
988
989                 if (CPU_ISSET_S(i, mask_size, cpu_mask))
990                         clos = 0;
991                 else
992                         clos = 3;
993
994                 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
995                 ret = isst_clos_associate(i, clos);
996                 if (ret) {
997                         perror("isst_clos_associate");
998                         return ret;
999                 }
1000         }
1001
1002         return 0;
1003 }
1004
1005 static int set_pbf_core_power(int cpu)
1006 {
1007         struct isst_pbf_info pbf_info;
1008         struct isst_pkg_ctdp pkg_dev;
1009         int ret;
1010
1011         ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1012         if (ret) {
1013                 perror("isst_get_ctdp_levels");
1014                 return ret;
1015         }
1016         debug_printf("Current_level: %d\n", pkg_dev.current_level);
1017
1018         ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1019         if (ret) {
1020                 perror("isst_get_pbf_info");
1021                 return ret;
1022         }
1023         debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1024                      pbf_info.p1_low);
1025
1026         ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1027                                         pbf_info.core_cpumask,
1028                                         pbf_info.p1_high, pbf_info.p1_low);
1029         if (ret) {
1030                 perror("set_core_priority_and_min");
1031                 return ret;
1032         }
1033
1034         ret = isst_pm_qos_config(cpu, 1, 1);
1035         if (ret) {
1036                 perror("isst_pm_qos_config");
1037                 return ret;
1038         }
1039
1040         return 0;
1041 }
1042
1043 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1044                             void *arg4)
1045 {
1046         int ret;
1047         int status = *(int *)arg4;
1048
1049         if (auto_mode) {
1050                 if (status) {
1051                         ret = set_pbf_core_power(cpu);
1052                         if (ret)
1053                                 goto disp_result;
1054                 } else {
1055                         isst_pm_qos_config(cpu, 0, 0);
1056                 }
1057         }
1058
1059         ret = isst_set_pbf_fact_status(cpu, 1, status);
1060         if (ret) {
1061                 perror("isst_set_pbf");
1062                 if (auto_mode)
1063                         isst_pm_qos_config(cpu, 0, 0);
1064         } else {
1065                 if (auto_mode) {
1066                         if (status)
1067                                 set_scaling_min_to_cpuinfo_max(cpu);
1068                         else
1069                                 set_scaling_min_to_cpuinfo_min(cpu);
1070                 }
1071         }
1072
1073 disp_result:
1074         if (status)
1075                 isst_display_result(cpu, outf, "base-freq", "enable",
1076                                     ret);
1077         else
1078                 isst_display_result(cpu, outf, "base-freq", "disable",
1079                                     ret);
1080 }
1081
1082 static void set_pbf_enable(void)
1083 {
1084         int status = 1;
1085
1086         if (cmd_help) {
1087                 fprintf(stderr,
1088                         "Enable Intel Speed Select Technology base frequency feature\n");
1089                 fprintf(stderr,
1090                         "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1091
1092                 exit(0);
1093         }
1094
1095         isst_ctdp_display_information_start(outf);
1096         if (max_target_cpus)
1097                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1098                                                   NULL, &status);
1099         else
1100                 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1101                                                NULL, &status);
1102         isst_ctdp_display_information_end(outf);
1103 }
1104
1105 static void set_pbf_disable(void)
1106 {
1107         int status = 0;
1108
1109         if (cmd_help) {
1110                 fprintf(stderr,
1111                         "Disable Intel Speed Select Technology base frequency feature\n");
1112                 fprintf(stderr,
1113                         "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1114                 exit(0);
1115         }
1116
1117         isst_ctdp_display_information_start(outf);
1118         if (max_target_cpus)
1119                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1120                                                   NULL, &status);
1121         else
1122                 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1123                                                NULL, &status);
1124         isst_ctdp_display_information_end(outf);
1125 }
1126
1127 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
1128                                      void *arg3, void *arg4)
1129 {
1130         struct isst_fact_info fact_info;
1131         int ret;
1132
1133         ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
1134         if (ret)
1135                 perror("isst_get_fact_bucket_info");
1136         else
1137                 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
1138                                               fact_avx, &fact_info);
1139 }
1140
1141 static void dump_fact_config(void)
1142 {
1143         if (cmd_help) {
1144                 fprintf(stderr,
1145                         "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
1146                 fprintf(stderr,
1147                         "\tArguments: -l|--level : Specify tdp level\n");
1148                 fprintf(stderr,
1149                         "\tArguments: -b|--bucket : Bucket index to dump\n");
1150                 fprintf(stderr,
1151                         "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
1152                 exit(0);
1153         }
1154
1155         if (tdp_level == 0xff) {
1156                 fprintf(outf, "Invalid command: specify tdp_level\n");
1157                 exit(1);
1158         }
1159
1160         isst_ctdp_display_information_start(outf);
1161         if (max_target_cpus)
1162                 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
1163                                                   NULL, NULL, NULL, NULL);
1164         else
1165                 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
1166                                                NULL, NULL, NULL);
1167         isst_ctdp_display_information_end(outf);
1168 }
1169
1170 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1171                              void *arg4)
1172 {
1173         int ret;
1174         int status = *(int *)arg4;
1175
1176         if (auto_mode) {
1177                 if (status) {
1178                         ret = isst_pm_qos_config(cpu, 1, 1);
1179                         if (ret)
1180                                 goto disp_results;
1181                 } else {
1182                         isst_pm_qos_config(cpu, 0, 0);
1183                 }
1184         }
1185
1186         ret = isst_set_pbf_fact_status(cpu, 0, status);
1187         if (ret) {
1188                 perror("isst_set_fact");
1189                 if (auto_mode)
1190                         isst_pm_qos_config(cpu, 0, 0);
1191
1192                 goto disp_results;
1193         }
1194
1195         /* Set TRL */
1196         if (status) {
1197                 struct isst_pkg_ctdp pkg_dev;
1198
1199                 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1200                 if (!ret)
1201                         ret = isst_set_trl(cpu, fact_trl);
1202                 if (ret && auto_mode)
1203                         isst_pm_qos_config(cpu, 0, 0);
1204         }
1205
1206 disp_results:
1207         if (status) {
1208                 isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
1209         } else {
1210                 /* Since we modified TRL during Fact enable, restore it */
1211                 isst_set_trl_from_current_tdp(cpu, fact_trl);
1212                 isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
1213         }
1214 }
1215
1216 static void set_fact_enable(void)
1217 {
1218         int status = 1, i, ret;
1219
1220         if (cmd_help) {
1221                 fprintf(stderr,
1222                         "Enable Intel Speed Select Technology Turbo frequency feature\n");
1223                 fprintf(stderr,
1224                         "Optional: -t|--trl : Specify turbo ratio limit\n");
1225                 fprintf(stderr,
1226                         "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1227                 fprintf(stderr, "-C|--cpu option as as high priority using core-power feature\n");
1228                 exit(0);
1229         }
1230
1231         isst_ctdp_display_information_start(outf);
1232         if (max_target_cpus)
1233                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1234                                                   NULL, &status);
1235         else
1236                 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1237                                                NULL, &status);
1238         isst_ctdp_display_information_end(outf);
1239
1240         if (auto_mode) {
1241                 /*
1242                  * When we adjust CLOS param, we have to set for siblings also.
1243                  * So for the each user specified CPU, also add the sibling
1244                  * in the present_cpu_mask.
1245                  */
1246                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1247                         char buffer[128], sibling_list[128], *cpu_str;
1248                         int fd, len;
1249
1250                         if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1251                                 continue;
1252
1253                         snprintf(buffer, sizeof(buffer),
1254                                  "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1255
1256                         fd = open(buffer, O_RDONLY);
1257                         if (fd < 0)
1258                                 continue;
1259
1260                         len = read(fd, sibling_list, sizeof(sibling_list));
1261                         close(fd);
1262
1263                         if (len < 0)
1264                                 continue;
1265
1266                         cpu_str = strtok(sibling_list, ",");
1267                         while (cpu_str != NULL) {
1268                                 int cpu;
1269
1270                                 sscanf(cpu_str, "%d", &cpu);
1271                                 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1272                                 cpu_str = strtok(NULL, ",");
1273                         }
1274                 }
1275
1276                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1277                         int clos;
1278
1279                         if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1280                                 continue;
1281
1282                         ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1283                         if (ret)
1284                                 goto error_disp;
1285
1286                         ret = set_clos_param(i, 1, 15, 0, 0, 0xff);
1287                         if (ret)
1288                                 goto error_disp;
1289
1290                         ret = set_clos_param(i, 2, 15, 0, 0, 0xff);
1291                         if (ret)
1292                                 goto error_disp;
1293
1294                         ret = set_clos_param(i, 3, 15, 0, 0, 0xff);
1295                         if (ret)
1296                                 goto error_disp;
1297
1298                         if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1299                                 clos = 0;
1300                         else
1301                                 clos = 3;
1302
1303                         debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1304                         ret = isst_clos_associate(i, clos);
1305                         if (ret)
1306                                 goto error_disp;
1307                 }
1308                 isst_display_result(i, outf, "turbo-freq --auto", "enable", 0);
1309         }
1310
1311         return;
1312
1313 error_disp:
1314         isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1315
1316 }
1317
1318 static void set_fact_disable(void)
1319 {
1320         int status = 0;
1321
1322         if (cmd_help) {
1323                 fprintf(stderr,
1324                         "Disable Intel Speed Select Technology turbo frequency feature\n");
1325                 fprintf(stderr,
1326                         "Optional: -t|--trl : Specify turbo ratio limit\n");
1327                 fprintf(stderr,
1328                         "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1329                 exit(0);
1330         }
1331
1332         isst_ctdp_display_information_start(outf);
1333         if (max_target_cpus)
1334                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1335                                                   NULL, &status);
1336         else
1337                 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1338                                                NULL, &status);
1339         isst_ctdp_display_information_end(outf);
1340 }
1341
1342 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
1343                                    void *arg4)
1344 {
1345         int ret;
1346         int status = *(int *)arg4;
1347
1348         ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1349         if (ret)
1350                 perror("isst_pm_qos_config");
1351
1352         if (status)
1353                 isst_display_result(cpu, outf, "core-power", "enable",
1354                                     ret);
1355         else
1356                 isst_display_result(cpu, outf, "core-power", "disable",
1357                                     ret);
1358 }
1359
1360 static void set_clos_enable(void)
1361 {
1362         int status = 1;
1363
1364         if (cmd_help) {
1365                 fprintf(stderr, "Enable core-power for a package/die\n");
1366                 fprintf(stderr,
1367                         "\tClos Enable: Specify priority type with [--priority|-p]\n");
1368                 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1369                 exit(0);
1370         }
1371
1372         if (cpufreq_sysfs_present()) {
1373                 fprintf(stderr,
1374                         "cpufreq subsystem and core-power enable will interfere with each other!\n");
1375         }
1376
1377         isst_ctdp_display_information_start(outf);
1378         if (max_target_cpus)
1379                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1380                                                   NULL, NULL, &status);
1381         else
1382                 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1383                                                NULL, NULL, &status);
1384         isst_ctdp_display_information_end(outf);
1385 }
1386
1387 static void set_clos_disable(void)
1388 {
1389         int status = 0;
1390
1391         if (cmd_help) {
1392                 fprintf(stderr,
1393                         "Disable core-power: [No command arguments are required]\n");
1394                 exit(0);
1395         }
1396
1397         isst_ctdp_display_information_start(outf);
1398         if (max_target_cpus)
1399                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1400                                                   NULL, NULL, &status);
1401         else
1402                 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1403                                                NULL, NULL, &status);
1404         isst_ctdp_display_information_end(outf);
1405 }
1406
1407 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1408                                      void *arg3, void *arg4)
1409 {
1410         struct isst_clos_config clos_config;
1411         int ret;
1412
1413         ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1414         if (ret)
1415                 perror("isst_pm_get_clos");
1416         else
1417                 isst_clos_display_information(cpu, outf, current_clos,
1418                                               &clos_config);
1419 }
1420
1421 static void dump_clos_config(void)
1422 {
1423         if (cmd_help) {
1424                 fprintf(stderr,
1425                         "Print Intel Speed Select Technology core power configuration\n");
1426                 fprintf(stderr,
1427                         "\tArguments: [-c | --clos]: Specify clos id\n");
1428                 exit(0);
1429         }
1430         if (current_clos < 0 || current_clos > 3) {
1431                 fprintf(stderr, "Invalid clos id\n");
1432                 exit(0);
1433         }
1434
1435         isst_ctdp_display_information_start(outf);
1436         if (max_target_cpus)
1437                 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
1438                                                   NULL, NULL, NULL, NULL);
1439         else
1440                 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
1441                                                NULL, NULL, NULL);
1442         isst_ctdp_display_information_end(outf);
1443 }
1444
1445 static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1446                                   void *arg4)
1447 {
1448         int enable, ret, prio_type;
1449
1450         ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
1451         if (ret)
1452                 perror("isst_clos_get_info");
1453         else
1454                 isst_clos_display_clos_information(cpu, outf, enable, prio_type);
1455 }
1456
1457 static void dump_clos_info(void)
1458 {
1459         if (cmd_help) {
1460                 fprintf(stderr,
1461                         "Print Intel Speed Select Technology core power information\n");
1462                 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
1463                 exit(0);
1464         }
1465
1466         if (!max_target_cpus) {
1467                 fprintf(stderr,
1468                         "Invalid target cpu. Specify with [-c|--cpu]\n");
1469                 exit(0);
1470         }
1471
1472         isst_ctdp_display_information_start(outf);
1473         for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
1474                                           NULL, NULL, NULL);
1475         isst_ctdp_display_information_end(outf);
1476
1477 }
1478
1479 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1480                                     void *arg4)
1481 {
1482         struct isst_clos_config clos_config;
1483         int ret;
1484
1485         clos_config.pkg_id = get_physical_package_id(cpu);
1486         clos_config.die_id = get_physical_die_id(cpu);
1487
1488         clos_config.epp = clos_epp;
1489         clos_config.clos_prop_prio = clos_prop_prio;
1490         clos_config.clos_min = clos_min;
1491         clos_config.clos_max = clos_max;
1492         clos_config.clos_desired = clos_desired;
1493         ret = isst_set_clos(cpu, current_clos, &clos_config);
1494         if (ret)
1495                 perror("isst_set_clos");
1496         else
1497                 isst_display_result(cpu, outf, "core-power", "config", ret);
1498 }
1499
1500 static void set_clos_config(void)
1501 {
1502         if (cmd_help) {
1503                 fprintf(stderr,
1504                         "Set core-power configuration for one of the four clos ids\n");
1505                 fprintf(stderr,
1506                         "\tSpecify targeted clos id with [--clos|-c]\n");
1507                 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
1508                 fprintf(stderr,
1509                         "\tSpecify clos Proportional Priority [--weight|-w]\n");
1510                 fprintf(stderr, "\tSpecify clos min with [--min|-n]\n");
1511                 fprintf(stderr, "\tSpecify clos max with [--max|-m]\n");
1512                 fprintf(stderr, "\tSpecify clos desired with [--desired|-d]\n");
1513                 exit(0);
1514         }
1515
1516         if (current_clos < 0 || current_clos > 3) {
1517                 fprintf(stderr, "Invalid clos id\n");
1518                 exit(0);
1519         }
1520         if (clos_epp < 0 || clos_epp > 0x0F) {
1521                 fprintf(stderr, "clos epp is not specified, default: 0\n");
1522                 clos_epp = 0;
1523         }
1524         if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
1525                 fprintf(stderr,
1526                         "clos frequency weight is not specified, default: 0\n");
1527                 clos_prop_prio = 0;
1528         }
1529         if (clos_min < 0) {
1530                 fprintf(stderr, "clos min is not specified, default: 0\n");
1531                 clos_min = 0;
1532         }
1533         if (clos_max < 0) {
1534                 fprintf(stderr, "clos max is not specified, default: 0xff\n");
1535                 clos_max = 0xff;
1536         }
1537         if (clos_desired < 0) {
1538                 fprintf(stderr, "clos desired is not specified, default: 0\n");
1539                 clos_desired = 0x00;
1540         }
1541
1542         isst_ctdp_display_information_start(outf);
1543         if (max_target_cpus)
1544                 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
1545                                                   NULL, NULL, NULL);
1546         else
1547                 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
1548                                                NULL, NULL, NULL);
1549         isst_ctdp_display_information_end(outf);
1550 }
1551
1552 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1553                                    void *arg4)
1554 {
1555         int ret;
1556
1557         ret = isst_clos_associate(cpu, current_clos);
1558         if (ret)
1559                 perror("isst_clos_associate");
1560         else
1561                 isst_display_result(cpu, outf, "core-power", "assoc", ret);
1562 }
1563
1564 static void set_clos_assoc(void)
1565 {
1566         if (cmd_help) {
1567                 fprintf(stderr, "Associate a clos id to a CPU\n");
1568                 fprintf(stderr,
1569                         "\tSpecify targeted clos id with [--clos|-c]\n");
1570                 exit(0);
1571         }
1572
1573         if (current_clos < 0 || current_clos > 3) {
1574                 fprintf(stderr, "Invalid clos id\n");
1575                 exit(0);
1576         }
1577         if (max_target_cpus)
1578                 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
1579                                                   NULL, NULL, NULL);
1580         else {
1581                 fprintf(stderr,
1582                         "Invalid target cpu. Specify with [-c|--cpu]\n");
1583         }
1584 }
1585
1586 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1587                                    void *arg4)
1588 {
1589         int clos, ret;
1590
1591         ret = isst_clos_get_assoc_status(cpu, &clos);
1592         if (ret)
1593                 perror("isst_clos_get_assoc_status");
1594         else
1595                 isst_clos_display_assoc_information(cpu, outf, clos);
1596 }
1597
1598 static void get_clos_assoc(void)
1599 {
1600         if (cmd_help) {
1601                 fprintf(stderr, "Get associate clos id to a CPU\n");
1602                 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
1603                 exit(0);
1604         }
1605
1606         if (!max_target_cpus) {
1607                 fprintf(stderr,
1608                         "Invalid target cpu. Specify with [-c|--cpu]\n");
1609                 exit(0);
1610         }
1611
1612         isst_ctdp_display_information_start(outf);
1613         for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
1614                                           NULL, NULL, NULL);
1615         isst_ctdp_display_information_end(outf);
1616 }
1617
1618 static struct process_cmd_struct isst_cmds[] = {
1619         { "perf-profile", "get-lock-status", get_tdp_locked },
1620         { "perf-profile", "get-config-levels", get_tdp_levels },
1621         { "perf-profile", "get-config-version", get_tdp_version },
1622         { "perf-profile", "get-config-enabled", get_tdp_enabled },
1623         { "perf-profile", "get-config-current-level", get_tdp_current_level },
1624         { "perf-profile", "set-config-level", set_tdp_level },
1625         { "perf-profile", "info", dump_isst_config },
1626         { "base-freq", "info", dump_pbf_config },
1627         { "base-freq", "enable", set_pbf_enable },
1628         { "base-freq", "disable", set_pbf_disable },
1629         { "turbo-freq", "info", dump_fact_config },
1630         { "turbo-freq", "enable", set_fact_enable },
1631         { "turbo-freq", "disable", set_fact_disable },
1632         { "core-power", "info", dump_clos_info },
1633         { "core-power", "enable", set_clos_enable },
1634         { "core-power", "disable", set_clos_disable },
1635         { "core-power", "config", set_clos_config },
1636         { "core-power", "get-config", dump_clos_config },
1637         { "core-power", "assoc", set_clos_assoc },
1638         { "core-power", "get-assoc", get_clos_assoc },
1639         { NULL, NULL, NULL }
1640 };
1641
1642 /*
1643  * parse cpuset with following syntax
1644  * 1,2,4..6,8-10 and set bits in cpu_subset
1645  */
1646 void parse_cpu_command(char *optarg)
1647 {
1648         unsigned int start, end;
1649         char *next;
1650
1651         next = optarg;
1652
1653         while (next && *next) {
1654                 if (*next == '-') /* no negative cpu numbers */
1655                         goto error;
1656
1657                 start = strtoul(next, &next, 10);
1658
1659                 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1660                         target_cpus[max_target_cpus++] = start;
1661
1662                 if (*next == '\0')
1663                         break;
1664
1665                 if (*next == ',') {
1666                         next += 1;
1667                         continue;
1668                 }
1669
1670                 if (*next == '-') {
1671                         next += 1; /* start range */
1672                 } else if (*next == '.') {
1673                         next += 1;
1674                         if (*next == '.')
1675                                 next += 1; /* start range */
1676                         else
1677                                 goto error;
1678                 }
1679
1680                 end = strtoul(next, &next, 10);
1681                 if (end <= start)
1682                         goto error;
1683
1684                 while (++start <= end) {
1685                         if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1686                                 target_cpus[max_target_cpus++] = start;
1687                 }
1688
1689                 if (*next == ',')
1690                         next += 1;
1691                 else if (*next != '\0')
1692                         goto error;
1693         }
1694
1695 #ifdef DEBUG
1696         {
1697                 int i;
1698
1699                 for (i = 0; i < max_target_cpus; ++i)
1700                         printf("cpu [%d] in arg\n", target_cpus[i]);
1701         }
1702 #endif
1703         return;
1704
1705 error:
1706         fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
1707         exit(-1);
1708 }
1709
1710 static void parse_cmd_args(int argc, int start, char **argv)
1711 {
1712         int opt;
1713         int option_index;
1714
1715         static struct option long_options[] = {
1716                 { "bucket", required_argument, 0, 'b' },
1717                 { "level", required_argument, 0, 'l' },
1718                 { "online", required_argument, 0, 'o' },
1719                 { "trl-type", required_argument, 0, 'r' },
1720                 { "trl", required_argument, 0, 't' },
1721                 { "help", no_argument, 0, 'h' },
1722                 { "clos", required_argument, 0, 'c' },
1723                 { "desired", required_argument, 0, 'd' },
1724                 { "epp", required_argument, 0, 'e' },
1725                 { "min", required_argument, 0, 'n' },
1726                 { "max", required_argument, 0, 'm' },
1727                 { "priority", required_argument, 0, 'p' },
1728                 { "weight", required_argument, 0, 'w' },
1729                 { "auto", no_argument, 0, 'a' },
1730                 { 0, 0, 0, 0 }
1731         };
1732
1733         option_index = start;
1734
1735         optind = start + 1;
1736         while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:hoa",
1737                                   long_options, &option_index)) != -1) {
1738                 switch (opt) {
1739                 case 'a':
1740                         auto_mode = 1;
1741                         break;
1742                 case 'b':
1743                         fact_bucket = atoi(optarg);
1744                         break;
1745                 case 'h':
1746                         cmd_help = 1;
1747                         break;
1748                 case 'l':
1749                         tdp_level = atoi(optarg);
1750                         break;
1751                 case 'o':
1752                         force_online_offline = 1;
1753                         break;
1754                 case 't':
1755                         sscanf(optarg, "0x%llx", &fact_trl);
1756                         break;
1757                 case 'r':
1758                         if (!strncmp(optarg, "sse", 3)) {
1759                                 fact_avx = 0x01;
1760                         } else if (!strncmp(optarg, "avx2", 4)) {
1761                                 fact_avx = 0x02;
1762                         } else if (!strncmp(optarg, "avx512", 4)) {
1763                                 fact_avx = 0x04;
1764                         } else {
1765                                 fprintf(outf, "Invalid sse,avx options\n");
1766                                 exit(1);
1767                         }
1768                         break;
1769                 /* CLOS related */
1770                 case 'c':
1771                         current_clos = atoi(optarg);
1772                         break;
1773                 case 'd':
1774                         clos_desired = atoi(optarg);
1775                         break;
1776                 case 'e':
1777                         clos_epp = atoi(optarg);
1778                         break;
1779                 case 'n':
1780                         clos_min = atoi(optarg);
1781                         break;
1782                 case 'm':
1783                         clos_max = atoi(optarg);
1784                         break;
1785                 case 'p':
1786                         clos_priority_type = atoi(optarg);
1787                         break;
1788                 case 'w':
1789                         clos_prop_prio = atoi(optarg);
1790                         break;
1791                 default:
1792                         printf("no match\n");
1793                 }
1794         }
1795 }
1796
1797 static void isst_help(void)
1798 {
1799         printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
1800                 performance profiles per system via static and/or dynamic\n\
1801                 adjustment of core count, workload, Tjmax, and\n\
1802                 TDP, etc.\n");
1803         printf("\nCommands : For feature=perf-profile\n");
1804         printf("\tinfo\n");
1805         printf("\tget-lock-status\n");
1806         printf("\tget-config-levels\n");
1807         printf("\tget-config-version\n");
1808         printf("\tget-config-enabled\n");
1809         printf("\tget-config-current-level\n");
1810         printf("\tset-config-level\n");
1811 }
1812
1813 static void pbf_help(void)
1814 {
1815         printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
1816                 on certain cores (high priority cores) in exchange for lower\n\
1817                 base frequency on remaining cores (low priority cores).\n");
1818         printf("\tcommand : info\n");
1819         printf("\tcommand : enable\n");
1820         printf("\tcommand : disable\n");
1821 }
1822
1823 static void fact_help(void)
1824 {
1825         printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
1826                 limits to cores based on priority.\n");
1827         printf("\nCommand: For feature=turbo-freq\n");
1828         printf("\tcommand : info\n");
1829         printf("\tcommand : enable\n");
1830         printf("\tcommand : disable\n");
1831 }
1832
1833 static void core_power_help(void)
1834 {
1835         printf("core-power:\tInterface that allows user to define per core/tile\n\
1836                 priority.\n");
1837         printf("\nCommands : For feature=core-power\n");
1838         printf("\tinfo\n");
1839         printf("\tenable\n");
1840         printf("\tdisable\n");
1841         printf("\tconfig\n");
1842         printf("\tget-config\n");
1843         printf("\tassoc\n");
1844         printf("\tget-assoc\n");
1845 }
1846
1847 struct process_cmd_help_struct {
1848         char *feature;
1849         void (*process_fn)(void);
1850 };
1851
1852 static struct process_cmd_help_struct isst_help_cmds[] = {
1853         { "perf-profile", isst_help },
1854         { "base-freq", pbf_help },
1855         { "turbo-freq", fact_help },
1856         { "core-power", core_power_help },
1857         { NULL, NULL }
1858 };
1859
1860 void process_command(int argc, char **argv)
1861 {
1862         int i = 0, matched = 0;
1863         char *feature = argv[optind];
1864         char *cmd = argv[optind + 1];
1865
1866         if (!feature || !cmd)
1867                 return;
1868
1869         debug_printf("feature name [%s] command [%s]\n", feature, cmd);
1870         if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
1871                 while (isst_help_cmds[i].feature) {
1872                         if (!strcmp(isst_help_cmds[i].feature, feature)) {
1873                                 isst_help_cmds[i].process_fn();
1874                                 exit(0);
1875                         }
1876                         ++i;
1877                 }
1878         }
1879
1880         create_cpu_map();
1881
1882         i = 0;
1883         while (isst_cmds[i].feature) {
1884                 if (!strcmp(isst_cmds[i].feature, feature) &&
1885                     !strcmp(isst_cmds[i].command, cmd)) {
1886                         parse_cmd_args(argc, optind + 1, argv);
1887                         isst_cmds[i].process_fn();
1888                         matched = 1;
1889                         break;
1890                 }
1891                 ++i;
1892         }
1893
1894         if (!matched)
1895                 fprintf(stderr, "Invalid command\n");
1896 }
1897
1898 static void usage(void)
1899 {
1900         printf("Intel(R) Speed Select Technology\n");
1901         printf("\nUsage:\n");
1902         printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
1903         printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features,\n");
1904         printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
1905         printf("\nFor help on each feature, use -h|--help\n");
1906         printf("\tFor example:  intel-speed-select perf-profile -h\n");
1907
1908         printf("\nFor additional help on each command for a feature, use --h|--help\n");
1909         printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
1910         printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
1911
1912         printf("\nOPTIONS\n");
1913         printf("\t[-c|--cpu] : logical cpu number\n");
1914         printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
1915         printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
1916         printf("\t[-d|--debug] : Debug mode\n");
1917         printf("\t[-h|--help] : Print help\n");
1918         printf("\t[-i|--info] : Print platform information\n");
1919         printf("\t[-o|--out] : Output file\n");
1920         printf("\t\t\tDefault : stderr\n");
1921         printf("\t[-f|--format] : output format [json|text]. Default: text\n");
1922         printf("\t[-v|--version] : Print version\n");
1923
1924         printf("\nResult format\n");
1925         printf("\tResult display uses a common format for each command:\n");
1926         printf("\tResults are formatted in text/JSON with\n");
1927         printf("\t\tPackage, Die, CPU, and command specific results.\n");
1928         exit(1);
1929 }
1930
1931 static void print_version(void)
1932 {
1933         fprintf(outf, "Version %s\n", version_str);
1934         fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
1935         exit(0);
1936 }
1937
1938 static void cmdline(int argc, char **argv)
1939 {
1940         int opt;
1941         int option_index = 0;
1942         int ret;
1943
1944         static struct option long_options[] = {
1945                 { "cpu", required_argument, 0, 'c' },
1946                 { "debug", no_argument, 0, 'd' },
1947                 { "format", required_argument, 0, 'f' },
1948                 { "help", no_argument, 0, 'h' },
1949                 { "info", no_argument, 0, 'i' },
1950                 { "out", required_argument, 0, 'o' },
1951                 { "version", no_argument, 0, 'v' },
1952                 { 0, 0, 0, 0 }
1953         };
1954
1955         progname = argv[0];
1956         while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
1957                                        &option_index)) != -1) {
1958                 switch (opt) {
1959                 case 'c':
1960                         parse_cpu_command(optarg);
1961                         break;
1962                 case 'd':
1963                         debug_flag = 1;
1964                         printf("Debug Mode ON\n");
1965                         break;
1966                 case 'f':
1967                         if (!strncmp(optarg, "json", 4))
1968                                 out_format_json = 1;
1969                         break;
1970                 case 'h':
1971                         usage();
1972                         break;
1973                 case 'i':
1974                         isst_print_platform_information();
1975                         break;
1976                 case 'o':
1977                         if (outf)
1978                                 fclose(outf);
1979                         outf = fopen_or_exit(optarg, "w");
1980                         break;
1981                 case 'v':
1982                         print_version();
1983                         break;
1984                 default:
1985                         usage();
1986                 }
1987         }
1988
1989         if (geteuid() != 0) {
1990                 fprintf(stderr, "Must run as root\n");
1991                 exit(0);
1992         }
1993
1994         if (optind > (argc - 2)) {
1995                 fprintf(stderr, "Feature name and|or command not specified\n");
1996                 exit(0);
1997         }
1998         update_cpu_model();
1999         printf("Intel(R) Speed Select Technology\n");
2000         printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2001         set_max_cpu_num();
2002         set_cpu_present_cpu_mask();
2003         set_cpu_target_cpu_mask();
2004         ret = isst_fill_platform_info();
2005         if (ret)
2006                 goto out;
2007
2008         process_command(argc, argv);
2009 out:
2010         free_cpu_set(present_cpumask);
2011         free_cpu_set(target_cpumask);
2012 }
2013
2014 int main(int argc, char **argv)
2015 {
2016         outf = stderr;
2017         cmdline(argc, argv);
2018         return 0;
2019 }