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