136da390b4b20ce44bf707bce0f1762b3e3e4ee9
[platform/kernel/linux-starfive.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.14";
19
20 static const int supported_api_ver = 1;
21 static struct isst_if_platform_info isst_platform_info;
22 static char *progname;
23 static int debug_flag;
24 static FILE *outf;
25
26 static int cpu_model;
27 static int cpu_stepping;
28
29 #define MAX_CPUS_IN_ONE_REQ 256
30 static short max_target_cpus;
31 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
32
33 static int topo_max_cpus;
34 static size_t present_cpumask_size;
35 static cpu_set_t *present_cpumask;
36 static size_t target_cpumask_size;
37 static cpu_set_t *target_cpumask;
38 static int tdp_level = 0xFF;
39 static int fact_bucket = 0xFF;
40 static int fact_avx = 0xFF;
41 static unsigned long long fact_trl;
42 static int out_format_json;
43 static int cmd_help;
44 static int force_online_offline;
45 static int auto_mode;
46 static int fact_enable_fail;
47
48 /* clos related */
49 static int current_clos = -1;
50 static int clos_epp = -1;
51 static int clos_prop_prio = -1;
52 static int clos_min = -1;
53 static int clos_max = -1;
54 static int clos_desired = -1;
55 static int clos_priority_type;
56
57 struct _cpu_map {
58         unsigned short core_id;
59         unsigned short pkg_id;
60         unsigned short die_id;
61         unsigned short punit_id;
62         unsigned short punit_cpu;
63         unsigned short punit_cpu_core;
64         unsigned short initialized;
65 };
66 struct _cpu_map *cpu_map;
67
68 struct cpu_topology {
69         short cpu;
70         short core_id;
71         short pkg_id;
72         short die_id;
73 };
74
75 FILE *get_output_file(void)
76 {
77         return outf;
78 }
79
80 int is_debug_enabled(void)
81 {
82         return debug_flag;
83 }
84
85 void debug_printf(const char *format, ...)
86 {
87         va_list args;
88
89         va_start(args, format);
90
91         if (debug_flag)
92                 vprintf(format, args);
93
94         va_end(args);
95 }
96
97
98 int is_clx_n_platform(void)
99 {
100         if (cpu_model == 0x55)
101                 if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
102                         return 1;
103         return 0;
104 }
105
106 int is_skx_based_platform(void)
107 {
108         if (cpu_model == 0x55)
109                 return 1;
110
111         return 0;
112 }
113
114 int is_spr_platform(void)
115 {
116         if (cpu_model == 0x8F || cpu_model == 0xCF)
117                 return 1;
118
119         return 0;
120 }
121
122 int is_icx_platform(void)
123 {
124         if (cpu_model == 0x6A || cpu_model == 0x6C)
125                 return 1;
126
127         return 0;
128 }
129
130 static int update_cpu_model(void)
131 {
132         unsigned int ebx, ecx, edx;
133         unsigned int fms, family;
134
135         __cpuid(1, fms, ebx, ecx, edx);
136         family = (fms >> 8) & 0xf;
137         cpu_model = (fms >> 4) & 0xf;
138         if (family == 6 || family == 0xf)
139                 cpu_model += ((fms >> 16) & 0xf) << 4;
140
141         cpu_stepping = fms & 0xf;
142         /* only three CascadeLake-N models are supported */
143         if (is_clx_n_platform()) {
144                 FILE *fp;
145                 size_t n = 0;
146                 char *line = NULL;
147                 int ret = 1;
148
149                 fp = fopen("/proc/cpuinfo", "r");
150                 if (!fp)
151                         err(-1, "cannot open /proc/cpuinfo\n");
152
153                 while (getline(&line, &n, fp) > 0) {
154                         if (strstr(line, "model name")) {
155                                 if (strstr(line, "6252N") ||
156                                     strstr(line, "6230N") ||
157                                     strstr(line, "5218N"))
158                                         ret = 0;
159                                 break;
160                         }
161                 }
162                 free(line);
163                 fclose(fp);
164                 return ret;
165         }
166         return 0;
167 }
168
169 int api_version(void)
170 {
171         return isst_platform_info.api_version;
172 }
173
174 /* Open a file, and exit on failure */
175 static FILE *fopen_or_exit(const char *path, const char *mode)
176 {
177         FILE *filep = fopen(path, mode);
178
179         if (!filep)
180                 err(1, "%s: open failed", path);
181
182         return filep;
183 }
184
185 /* Parse a file containing a single int */
186 static int parse_int_file(int fatal, const char *fmt, ...)
187 {
188         va_list args;
189         char path[PATH_MAX];
190         FILE *filep;
191         int value;
192
193         va_start(args, fmt);
194         vsnprintf(path, sizeof(path), fmt, args);
195         va_end(args);
196         if (fatal) {
197                 filep = fopen_or_exit(path, "r");
198         } else {
199                 filep = fopen(path, "r");
200                 if (!filep)
201                         return -1;
202         }
203         if (fscanf(filep, "%d", &value) != 1)
204                 err(1, "%s: failed to parse number from file", path);
205         fclose(filep);
206
207         return value;
208 }
209
210 int cpufreq_sysfs_present(void)
211 {
212         DIR *dir;
213
214         dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
215         if (dir) {
216                 closedir(dir);
217                 return 1;
218         }
219
220         return 0;
221 }
222
223 int out_format_is_json(void)
224 {
225         return out_format_json;
226 }
227
228 static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
229 {
230         const char *pathname = "/var/run/isst_cpu_topology.dat";
231         struct cpu_topology cpu_top;
232         FILE *fp;
233         int ret;
234
235         fp = fopen(pathname, "rb");
236         if (!fp)
237                 return -1;
238
239         ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
240         if (ret)
241                 goto err_ret;
242
243         ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
244         if (ret != 1) {
245                 ret = -1;
246                 goto err_ret;
247         }
248
249         *pkg_id = cpu_top.pkg_id;
250         *core_id = cpu_top.core_id;
251         *die_id = cpu_top.die_id;
252         ret = 0;
253
254 err_ret:
255         fclose(fp);
256
257         return ret;
258 }
259
260 static void store_cpu_topology(void)
261 {
262         const char *pathname = "/var/run/isst_cpu_topology.dat";
263         FILE *fp;
264         int i;
265
266         fp = fopen(pathname, "rb");
267         if (fp) {
268                 /* Mapping already exists */
269                 fclose(fp);
270                 return;
271         }
272
273         fp = fopen(pathname, "wb");
274         if (!fp) {
275                 fprintf(stderr, "Can't create file:%s\n", pathname);
276                 return;
277         }
278
279         fprintf(stderr, "Caching topology information\n");
280
281         for (i = 0; i < topo_max_cpus; ++i) {
282                 struct cpu_topology cpu_top;
283
284                 cpu_top.core_id = parse_int_file(0,
285                         "/sys/devices/system/cpu/cpu%d/topology/core_id", i);
286                 if (cpu_top.core_id < 0)
287                         cpu_top.core_id = -1;
288
289                 cpu_top.pkg_id = parse_int_file(0,
290                         "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
291                 if (cpu_top.pkg_id < 0)
292                         cpu_top.pkg_id = -1;
293
294                 cpu_top.die_id = parse_int_file(0,
295                         "/sys/devices/system/cpu/cpu%d/topology/die_id", i);
296                 if (cpu_top.die_id < 0)
297                         cpu_top.die_id = -1;
298
299                 cpu_top.cpu = i;
300
301                 if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
302                         fprintf(stderr, "Can't write to:%s\n", pathname);
303                         break;
304                 }
305         }
306
307         fclose(fp);
308 }
309
310 static int get_physical_package_id(int cpu)
311 {
312         int ret;
313
314         if (cpu < 0)
315                 return -1;
316
317         if (cpu_map && cpu_map[cpu].initialized)
318                 return cpu_map[cpu].pkg_id;
319
320         ret = parse_int_file(0,
321                         "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
322                         cpu);
323         if (ret < 0) {
324                 int core_id, pkg_id, die_id;
325
326                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
327                 if (!ret)
328                         return pkg_id;
329         }
330
331         return ret;
332 }
333
334 static int get_physical_core_id(int cpu)
335 {
336         int ret;
337
338         if (cpu < 0)
339                 return -1;
340
341         if (cpu_map && cpu_map[cpu].initialized)
342                 return cpu_map[cpu].core_id;
343
344         ret = parse_int_file(0,
345                         "/sys/devices/system/cpu/cpu%d/topology/core_id",
346                         cpu);
347         if (ret < 0) {
348                 int core_id, pkg_id, die_id;
349
350                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
351                 if (!ret)
352                         return core_id;
353         }
354
355         return ret;
356 }
357
358 static int get_physical_die_id(int cpu)
359 {
360         int ret;
361
362         if (cpu < 0)
363                 return -1;
364
365         if (cpu_map && cpu_map[cpu].initialized)
366                 return cpu_map[cpu].die_id;
367
368         ret = parse_int_file(0,
369                         "/sys/devices/system/cpu/cpu%d/topology/die_id",
370                         cpu);
371         if (ret < 0) {
372                 int core_id, pkg_id, die_id;
373
374                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
375                 if (!ret) {
376                         if (die_id < 0)
377                                 die_id = 0;
378
379                         return die_id;
380                 }
381         }
382
383         if (ret < 0)
384                 ret = 0;
385
386         return ret;
387 }
388
389 static int get_physical_punit_id(int cpu)
390 {
391         if (cpu < 0)
392                 return -1;
393
394         if (cpu_map && cpu_map[cpu].initialized)
395                 return cpu_map[cpu].punit_id;
396
397         return -1;
398 }
399
400 void set_isst_id(struct isst_id *id, int cpu)
401 {
402         id->cpu = cpu;
403
404         id->pkg = get_physical_package_id(cpu);
405         if (id->pkg >= MAX_PACKAGE_COUNT)
406                 id->pkg = -1;
407
408         id->die = get_physical_die_id(cpu);
409         if (id->die >= MAX_DIE_PER_PACKAGE)
410                 id->die = -1;
411
412         id->punit = get_physical_punit_id(cpu);
413         if (id->punit >= MAX_PUNIT_PER_DIE)
414                 id->punit = -1;
415 }
416
417 int is_cpu_in_power_domain(int cpu, struct isst_id *id)
418 {
419         struct isst_id tid;
420
421         set_isst_id(&tid, cpu);
422
423         if (id->pkg == tid.pkg && id->die == tid.die && id->punit == tid.punit)
424                 return 1;
425
426         return 0;
427 }
428
429 int get_cpufreq_base_freq(int cpu)
430 {
431         return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
432 }
433
434 int get_topo_max_cpus(void)
435 {
436         return topo_max_cpus;
437 }
438
439 static unsigned int is_cpu_online(int cpu)
440 {
441         char buffer[128];
442         int fd, ret;
443         unsigned char online;
444
445         snprintf(buffer, sizeof(buffer),
446                  "/sys/devices/system/cpu/cpu%d/online", cpu);
447
448         fd = open(buffer, O_RDONLY);
449         if (fd < 0)
450                 return fd;
451
452         ret = read(fd, &online, sizeof(online));
453         close(fd);
454
455         if (ret == -1)
456                 return ret;
457
458         if (online == '1')
459                 online = 1;
460         else
461                 online = 0;
462
463         return online;
464 }
465
466 void set_cpu_online_offline(int cpu, int state)
467 {
468         char buffer[128];
469         int fd, ret;
470
471         snprintf(buffer, sizeof(buffer),
472                  "/sys/devices/system/cpu/cpu%d/online", cpu);
473
474         fd = open(buffer, O_WRONLY);
475         if (fd < 0) {
476                 if (!cpu && state) {
477                         fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
478                         fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
479                         return;
480                 }
481                 err(-1, "%s open failed", buffer);
482         }
483
484         if (state)
485                 ret = write(fd, "1\n", 2);
486         else
487                 ret = write(fd, "0\n", 2);
488
489         if (ret == -1)
490                 perror("Online/Offline: Operation failed\n");
491
492         close(fd);
493 }
494
495 static void force_all_cpus_online(void)
496 {
497         int i;
498
499         fprintf(stderr, "Forcing all CPUs online\n");
500
501         for (i = 0; i < topo_max_cpus; ++i)
502                 set_cpu_online_offline(i, 1);
503
504         unlink("/var/run/isst_cpu_topology.dat");
505 }
506
507 void for_each_online_power_domain_in_set(void (*callback)(struct isst_id *, void *, void *,
508                                                      void *, void *),
509                                     void *arg1, void *arg2, void *arg3,
510                                     void *arg4)
511 {
512         struct isst_id id;
513         int cpus[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
514         int valid_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE] = {0};
515         int i, j, k;
516
517         memset(cpus, -1, sizeof(cpus));
518
519         for (i = 0; i < topo_max_cpus; ++i) {
520                 int online;
521
522                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
523                         continue;
524
525                 online = parse_int_file(
526                         i != 0, "/sys/devices/system/cpu/cpu%d/online", i);
527                 if (online < 0)
528                         online = 1; /* online entry for CPU 0 needs some special configs */
529
530                 if (!online)
531                         continue;
532
533                 set_isst_id(&id, i);
534
535                 if (id.pkg < 0 || id.die < 0 || id.punit < 0)
536                         continue;
537
538                 valid_mask[id.pkg][id.die] = 1;
539
540                 if (cpus[id.pkg][id.die][id.punit] == -1)
541                         cpus[id.pkg][id.die][id.punit] = i;
542         }
543
544         for (i = 0; i < MAX_PACKAGE_COUNT; i++) {
545                 for (j = 0; j < MAX_DIE_PER_PACKAGE; j++) {
546                         /*
547                          * Fix me:
548                          * How to check a non-cpu die for a package/die with all cpu offlined?
549                          */
550                         if (!valid_mask[i][j])
551                                 continue;
552                         for (k = 0; k < MAX_PUNIT_PER_DIE; k++) {
553                                 id.cpu = cpus[i][j][k];
554                                 id.pkg = i;
555                                 id.die = j;
556                                 id.punit = k;
557                                 if (isst_is_punit_valid(&id))
558                                         callback(&id, arg1, arg2, arg3, arg4);
559                         }
560                 }
561         }
562 }
563
564 static void for_each_online_target_cpu_in_set(
565         void (*callback)(struct isst_id *, void *, void *, void *, void *), void *arg1,
566         void *arg2, void *arg3, void *arg4)
567 {
568         int i, found = 0;
569         struct isst_id id;
570
571         for (i = 0; i < topo_max_cpus; ++i) {
572                 int online;
573
574                 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
575                         continue;
576                 if (i)
577                         online = parse_int_file(
578                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
579                 else
580                         online =
581                                 1; /* online entry for CPU 0 needs some special configs */
582
583                 set_isst_id(&id, i);
584                 if (online && callback) {
585                         callback(&id, arg1, arg2, arg3, arg4);
586                         found = 1;
587                 }
588         }
589
590         if (!found)
591                 fprintf(stderr, "No valid CPU in the list\n");
592 }
593
594 #define BITMASK_SIZE 32
595 static void set_max_cpu_num(void)
596 {
597         FILE *filep;
598         unsigned long dummy;
599         int i;
600
601         topo_max_cpus = 0;
602         for (i = 0; i < 256; ++i) {
603                 char path[256];
604
605                 snprintf(path, sizeof(path),
606                          "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
607                 filep = fopen(path, "r");
608                 if (filep)
609                         break;
610         }
611
612         if (!filep) {
613                 fprintf(stderr, "Can't get max cpu number\n");
614                 exit(0);
615         }
616
617         while (fscanf(filep, "%lx,", &dummy) == 1)
618                 topo_max_cpus += BITMASK_SIZE;
619         fclose(filep);
620
621         debug_printf("max cpus %d\n", topo_max_cpus);
622 }
623
624 size_t alloc_cpu_set(cpu_set_t **cpu_set)
625 {
626         cpu_set_t *_cpu_set;
627         size_t size;
628
629         _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
630         if (_cpu_set == NULL)
631                 err(3, "CPU_ALLOC");
632         size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
633         CPU_ZERO_S(size, _cpu_set);
634
635         *cpu_set = _cpu_set;
636         return size;
637 }
638
639 void free_cpu_set(cpu_set_t *cpu_set)
640 {
641         CPU_FREE(cpu_set);
642 }
643
644 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
645
646 int get_max_punit_core_id(struct isst_id *id)
647 {
648         int max_id = 0;
649         int i;
650
651         for (i = 0; i < topo_max_cpus; ++i)
652         {
653                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
654                         continue;
655
656                 if (is_cpu_in_power_domain(i, id) &&
657                     cpu_map[i].punit_cpu_core > max_id)
658                         max_id = cpu_map[i].punit_cpu_core;
659         }
660
661         return max_id;
662 }
663
664 int get_cpu_count(struct isst_id *id)
665 {
666         if (id->pkg < 0 || id->die < 0 || id->punit < 0)
667                 return 0;
668
669         return cpu_cnt[id->pkg][id->die][id->punit];
670 }
671
672 static void update_punit_cpu_info(__u32 physical_cpu, struct _cpu_map *cpu_map)
673 {
674         if (api_version() > 1) {
675                 /*
676                  * MSR 0x54 format
677                  *      [15:11] PM_DOMAIN_ID
678                  *      [10:3] MODULE_ID (aka IDI_AGENT_ID)
679                  *      [2:0] LP_ID (We don't care about these bits we only
680                  *              care die and core id
681                  *      For Atom:
682                  *      [2] Always 0
683                  *      [1:0] core ID within module
684                  *      For Core
685                  *      [2:1] Always 0
686                  *      [0] thread ID
687                  */
688                 cpu_map->punit_id = (physical_cpu >> 11) & 0x1f;
689                 cpu_map->punit_cpu_core = (physical_cpu >> 3) & 0xff;
690                 cpu_map->punit_cpu = physical_cpu & 0x7ff;
691         } else {
692                 int punit_id;
693
694                 /*
695                  * MSR 0x53 format
696                  * Format
697                  *      Bit 0 â€“ thread ID
698                  *      Bit 8:1 â€“ core ID
699                  *      Bit 13:9 â€“ punit ID
700                  */
701                 cpu_map->punit_cpu = physical_cpu & 0x1ff;
702                 cpu_map->punit_cpu_core = (cpu_map->punit_cpu >> 1); // shift to get core id
703                 punit_id = (physical_cpu >> 9) & 0x1f;
704
705                 if (punit_id >= MAX_PUNIT_PER_DIE)
706                         punit_id = 0;
707
708                 cpu_map->punit_id = punit_id;
709         }
710 }
711
712 static void create_cpu_map(void)
713 {
714         const char *pathname = "/dev/isst_interface";
715         size_t size;
716         DIR *dir;
717         int i, fd = 0;
718         struct isst_if_cpu_maps map;
719
720         /* Use calloc to make sure the memory is initialized to Zero */
721         cpu_map = calloc(topo_max_cpus, sizeof(*cpu_map));
722         if (!cpu_map)
723                 err(3, "cpumap");
724
725         fd = open(pathname, O_RDWR);
726         if (fd < 0 && !is_clx_n_platform())
727                 err(-1, "%s open failed", pathname);
728
729         size = alloc_cpu_set(&present_cpumask);
730         present_cpumask_size = size;
731
732         for (i = 0; i < topo_max_cpus; ++i) {
733                 char buffer[256];
734                 int pkg_id, die_id, core_id, punit_id;
735
736                 /* check if CPU is online */
737                 snprintf(buffer, sizeof(buffer),
738                          "/sys/devices/system/cpu/cpu%d", i);
739                 dir = opendir(buffer);
740                 if (!dir)
741                         continue;
742                 closedir(dir);
743
744                 CPU_SET_S(i, size, present_cpumask);
745
746                 pkg_id = get_physical_package_id(i);
747                 die_id = get_physical_die_id(i);
748                 core_id = get_physical_core_id(i);
749
750                 if (pkg_id < 0 || die_id < 0 || core_id < 0)
751                         continue;
752
753                 cpu_map[i].pkg_id = pkg_id;
754                 cpu_map[i].die_id = die_id;
755                 cpu_map[i].core_id = core_id;
756
757
758                 punit_id = 0;
759
760                 if (fd >= 0) {
761                         map.cmd_count = 1;
762                         map.cpu_map[0].logical_cpu = i;
763                         debug_printf(" map logical_cpu:%d\n",
764                                      map.cpu_map[0].logical_cpu);
765                         if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
766                                 perror("ISST_IF_GET_PHY_ID");
767                                 fprintf(outf, "Error: map logical_cpu:%d\n",
768                                         map.cpu_map[0].logical_cpu);
769                         } else {
770                                 update_punit_cpu_info(map.cpu_map[0].physical_cpu, &cpu_map[i]);
771                         }
772                 }
773                 cpu_map[i].initialized = 1;
774
775                 cpu_cnt[pkg_id][die_id][punit_id]++;
776
777                 debug_printf(
778                         "map logical_cpu:%d core: %d die:%d pkg:%d punit:%d punit_cpu:%d punit_core:%d\n",
779                         i, cpu_map[i].core_id, cpu_map[i].die_id,
780                         cpu_map[i].pkg_id, cpu_map[i].punit_id,
781                         cpu_map[i].punit_cpu, cpu_map[i].punit_cpu_core);
782         }
783         if (fd >= 0)
784                 close(fd);
785
786         size = alloc_cpu_set(&target_cpumask);
787         target_cpumask_size = size;
788         for (i = 0; i < max_target_cpus; ++i) {
789                 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
790                                  present_cpumask))
791                         continue;
792
793                 CPU_SET_S(target_cpus[i], size, target_cpumask);
794         }
795 }
796
797 void set_cpu_mask_from_punit_coremask(struct isst_id *id, unsigned long long core_mask,
798                                       size_t core_cpumask_size,
799                                       cpu_set_t *core_cpumask, int *cpu_cnt)
800 {
801         int i, cnt = 0;
802
803         *cpu_cnt = 0;
804
805         for (i = 0; i < 64; ++i) {
806                 if (core_mask & BIT_ULL(i)) {
807                         int j;
808
809                         for (j = 0; j < topo_max_cpus; ++j) {
810                                 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
811                                         continue;
812
813                                 if (is_cpu_in_power_domain(j, id) &&
814                                     cpu_map[j].punit_cpu_core == i) {
815                                         CPU_SET_S(j, core_cpumask_size,
816                                                   core_cpumask);
817                                         ++cnt;
818                                 }
819                         }
820                 }
821         }
822
823         *cpu_cnt = cnt;
824 }
825
826 int find_phy_core_num(int logical_cpu)
827 {
828         if (logical_cpu < topo_max_cpus)
829                 return cpu_map[logical_cpu].punit_cpu_core;
830
831         return -EINVAL;
832 }
833
834
835
836 static int isst_fill_platform_info(void)
837 {
838         const char *pathname = "/dev/isst_interface";
839         int fd;
840
841         if (is_clx_n_platform()) {
842                 isst_platform_info.api_version = 1;
843                 goto set_platform_ops;
844         }
845
846         fd = open(pathname, O_RDWR);
847         if (fd < 0)
848                 err(-1, "%s open failed", pathname);
849
850         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
851                 perror("ISST_IF_GET_PLATFORM_INFO");
852                 close(fd);
853                 return -1;
854         }
855
856         close(fd);
857
858         if (isst_platform_info.api_version > supported_api_ver) {
859                 printf("Incompatible API versions; Upgrade of tool is required\n");
860                 return -1;
861         }
862
863 set_platform_ops:
864         if (isst_set_platform_ops(isst_platform_info.api_version)) {
865                 fprintf(stderr, "Failed to set platform callbacks\n");
866                 exit(0);
867         }
868         return 0;
869 }
870
871 void get_isst_status(struct isst_id *id, void *arg1, void *arg2, void *arg3, void *arg4)
872 {
873         struct isst_pkg_ctdp pkg_dev;
874         struct isst_id *tid = (struct isst_id *)arg2;
875         int *mask = (int *)arg3;
876         int *max_level = (int *)arg4;
877         int j, ret;
878
879         /* Only check the first cpu power domain */
880         if (id->cpu < 0 || tid->cpu >= 0)
881                 return;
882
883         ret = isst_get_ctdp_levels(id, &pkg_dev);
884         if (ret)
885                 return;
886
887         if (pkg_dev.enabled)
888                 *mask |= BIT(0);
889
890         if (pkg_dev.locked)
891                 *mask |= BIT(1);
892
893         if (*max_level < pkg_dev.levels)
894                 *max_level = pkg_dev.levels;
895
896         for (j = 0; j <= pkg_dev.levels; ++j) {
897                 struct isst_pkg_ctdp_level_info ctdp_level;
898
899                 ret = isst_get_ctdp_control(id, j, &ctdp_level);
900                 if (ret)
901                         continue;
902
903                 if (ctdp_level.fact_support)
904                         *mask |= BIT(2);
905
906                 if (ctdp_level.pbf_support)
907                         *mask |= BIT(3);
908         }
909
910         tid->cpu = id->cpu;
911         tid->pkg = id->pkg;
912         tid->die = id->die;
913         tid->punit = id->punit;
914 }
915
916 static void isst_print_extended_platform_info(void)
917 {
918         int cp_state, cp_cap;
919         struct isst_id id;
920         int mask = 0, max_level = 0;
921
922         id.cpu = -1;
923         for_each_online_power_domain_in_set(get_isst_status, NULL, &id, &mask, &max_level);
924
925         if (mask & BIT(0)) {
926                 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
927         } else {
928                 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
929                 fprintf(outf, "Only performance level 0 (base level) is present\n");
930         }
931
932         if (mask & BIT(1))
933                 fprintf(outf, "TDP level change control is locked\n");
934         else
935                 fprintf(outf, "TDP level change control is unlocked, max level: %d\n", max_level);
936
937         if (mask & BIT(2))
938                 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
939         else
940                 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
941
942         if (mask & BIT(3))
943                 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
944         else
945                 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
946
947         if (isst_read_pm_config(&id, &cp_state, &cp_cap)) {
948                 fprintf(outf, "Intel(R) SST-CP (feature core-power) status is unknown\n");
949                 return;
950         }
951
952         if (cp_cap)
953                 fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
954         else
955                 fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
956 }
957
958 static void isst_print_platform_information(void)
959 {
960         if (is_clx_n_platform()) {
961                 fprintf(stderr, "\nThis option in not supported on this platform\n");
962                 exit(0);
963         }
964
965         /* Early initialization to create working cpu_map */
966         set_max_cpu_num();
967         create_cpu_map();
968
969         fprintf(outf, "Platform: API version : %d\n",
970                 isst_platform_info.api_version);
971         fprintf(outf, "Platform: Driver version : %d\n",
972                 isst_platform_info.driver_version);
973         fprintf(outf, "Platform: mbox supported : %d\n",
974                 isst_platform_info.mbox_supported);
975         fprintf(outf, "Platform: mmio supported : %d\n",
976                 isst_platform_info.mmio_supported);
977         isst_print_extended_platform_info();
978
979         exit(0);
980 }
981
982 static char *local_str0, *local_str1;
983 static void exec_on_get_ctdp_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
984                                  void *arg4)
985 {
986         int (*fn_ptr)(struct isst_id *id, void *arg);
987         int ret;
988
989         fn_ptr = arg1;
990         ret = fn_ptr(id, arg2);
991         if (ret)
992                 isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
993         else
994                 isst_ctdp_display_core_info(id, outf, arg3,
995                                             *(unsigned int *)arg4,
996                                             local_str0, local_str1);
997 }
998
999 #define _get_tdp_level(desc, suffix, object, help, str0, str1)                  \
1000         static void get_tdp_##object(int arg)                                    \
1001         {                                                                         \
1002                 struct isst_pkg_ctdp ctdp;                                        \
1003 \
1004                 if (cmd_help) {                                                   \
1005                         fprintf(stderr,                                           \
1006                                 "Print %s [No command arguments are required]\n", \
1007                                 help);                                            \
1008                         exit(0);                                                  \
1009                 }                                                                 \
1010                 local_str0 = str0;                                                \
1011                 local_str1 = str1;                                                \
1012                 isst_ctdp_display_information_start(outf);                        \
1013                 if (max_target_cpus)                                              \
1014                         for_each_online_target_cpu_in_set(                        \
1015                                 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
1016                                 &ctdp, desc, &ctdp.object);                       \
1017                 else                                                              \
1018                         for_each_online_power_domain_in_set(exec_on_get_ctdp_cpu,      \
1019                                                        isst_get_ctdp_##suffix,    \
1020                                                        &ctdp, desc,               \
1021                                                        &ctdp.object);             \
1022                 isst_ctdp_display_information_end(outf);                          \
1023         }
1024
1025 _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
1026 _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
1027 _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
1028 _get_tdp_level("get-config-current_level", levels, current_level,
1029                "Current TDP Level", NULL, NULL);
1030 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
1031
1032 struct isst_pkg_ctdp clx_n_pkg_dev;
1033
1034 static int clx_n_get_base_ratio(void)
1035 {
1036         FILE *fp;
1037         char *begin, *end, *line = NULL;
1038         char number[5];
1039         float value = 0;
1040         size_t n = 0;
1041
1042         fp = fopen("/proc/cpuinfo", "r");
1043         if (!fp)
1044                 err(-1, "cannot open /proc/cpuinfo\n");
1045
1046         while (getline(&line, &n, fp) > 0) {
1047                 if (strstr(line, "model name")) {
1048                         /* this is true for CascadeLake-N */
1049                         begin = strstr(line, "@ ") + 2;
1050                         end = strstr(line, "GHz");
1051                         strncpy(number, begin, end - begin);
1052                         value = atof(number) * 10;
1053                         break;
1054                 }
1055         }
1056         free(line);
1057         fclose(fp);
1058
1059         return (int)(value);
1060 }
1061
1062 static int clx_n_config(struct isst_id *id)
1063 {
1064         int i, ret;
1065         unsigned long cpu_bf;
1066         struct isst_pkg_ctdp_level_info *ctdp_level;
1067         struct isst_pbf_info *pbf_info;
1068
1069         ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1070         pbf_info = &ctdp_level->pbf_info;
1071         ctdp_level->core_cpumask_size =
1072                         alloc_cpu_set(&ctdp_level->core_cpumask);
1073
1074         /* find the frequency base ratio */
1075         ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1076         if (ctdp_level->tdp_ratio == 0) {
1077                 debug_printf("CLX: cn base ratio is zero\n");
1078                 ret = -1;
1079                 goto error_ret;
1080         }
1081
1082         /* find the high and low priority frequencies */
1083         pbf_info->p1_high = 0;
1084         pbf_info->p1_low = ~0;
1085
1086         for (i = 0; i < topo_max_cpus; i++) {
1087                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1088                         continue;
1089
1090                 if (!is_cpu_in_power_domain(i, id))
1091                         continue;
1092
1093                 CPU_SET_S(i, ctdp_level->core_cpumask_size,
1094                           ctdp_level->core_cpumask);
1095
1096                 cpu_bf = parse_int_file(1,
1097                         "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1098                                         i);
1099                 if (cpu_bf > pbf_info->p1_high)
1100                         pbf_info->p1_high = cpu_bf;
1101                 if (cpu_bf < pbf_info->p1_low)
1102                         pbf_info->p1_low = cpu_bf;
1103         }
1104
1105         if (pbf_info->p1_high == ~0UL) {
1106                 debug_printf("CLX: maximum base frequency not set\n");
1107                 ret = -1;
1108                 goto error_ret;
1109         }
1110
1111         if (pbf_info->p1_low == 0) {
1112                 debug_printf("CLX: minimum base frequency not set\n");
1113                 ret = -1;
1114                 goto error_ret;
1115         }
1116
1117         /* convert frequencies back to ratios */
1118         pbf_info->p1_high = pbf_info->p1_high / 100000;
1119         pbf_info->p1_low = pbf_info->p1_low / 100000;
1120
1121         /* create high priority cpu mask */
1122         pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1123         for (i = 0; i < topo_max_cpus; i++) {
1124                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1125                         continue;
1126
1127                 if (!is_cpu_in_power_domain(i, id))
1128                         continue;
1129
1130                 cpu_bf = parse_int_file(1,
1131                         "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1132                                         i);
1133                 cpu_bf = cpu_bf / 100000;
1134                 if (cpu_bf == pbf_info->p1_high)
1135                         CPU_SET_S(i, pbf_info->core_cpumask_size,
1136                                   pbf_info->core_cpumask);
1137         }
1138
1139         /* extra ctdp & pbf struct parameters */
1140         ctdp_level->processed = 1;
1141         ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1142         ctdp_level->pbf_enabled = 1;
1143         ctdp_level->fact_support = 0; /* FACT is never supported */
1144         ctdp_level->fact_enabled = 0;
1145
1146         return 0;
1147
1148 error_ret:
1149         free_cpu_set(ctdp_level->core_cpumask);
1150         return ret;
1151 }
1152
1153 static void dump_clx_n_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1154                                    void *arg3, void *arg4)
1155 {
1156         int ret;
1157
1158         if (tdp_level != 0xff && tdp_level != 0) {
1159                 isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1160                 exit(0);
1161         }
1162
1163         ret = clx_n_config(id);
1164         if (ret) {
1165                 debug_printf("clx_n_config failed");
1166         } else {
1167                 struct isst_pkg_ctdp_level_info *ctdp_level;
1168                 struct isst_pbf_info *pbf_info;
1169
1170                 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1171                 pbf_info = &ctdp_level->pbf_info;
1172                 clx_n_pkg_dev.processed = 1;
1173                 isst_ctdp_display_information(id, outf, tdp_level, &clx_n_pkg_dev);
1174                 free_cpu_set(ctdp_level->core_cpumask);
1175                 free_cpu_set(pbf_info->core_cpumask);
1176         }
1177 }
1178
1179 static void dump_isst_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1180                                      void *arg3, void *arg4)
1181 {
1182         struct isst_pkg_ctdp pkg_dev;
1183         int ret;
1184
1185         memset(&pkg_dev, 0, sizeof(pkg_dev));
1186         ret = isst_get_process_ctdp(id, tdp_level, &pkg_dev);
1187         if (ret) {
1188                 isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, id->cpu);
1189                 isst_ctdp_display_information_end(outf);
1190                 exit(1);
1191         } else {
1192                 isst_ctdp_display_information(id, outf, tdp_level, &pkg_dev);
1193                 isst_get_process_ctdp_complete(id, &pkg_dev);
1194         }
1195 }
1196
1197 static void dump_isst_config(int arg)
1198 {
1199         void *fn;
1200
1201         if (cmd_help) {
1202                 fprintf(stderr,
1203                         "Print Intel(R) Speed Select Technology Performance profile configuration\n");
1204                 fprintf(stderr,
1205                         "including base frequency and turbo frequency configurations\n");
1206                 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
1207                 fprintf(stderr,
1208                         "\tIf no arguments, dump information for all TDP levels\n");
1209                 exit(0);
1210         }
1211
1212         if (!is_clx_n_platform())
1213                 fn = dump_isst_config_for_cpu;
1214         else
1215                 fn = dump_clx_n_config_for_cpu;
1216
1217         isst_ctdp_display_information_start(outf);
1218
1219         if (max_target_cpus)
1220                 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1221         else
1222                 for_each_online_power_domain_in_set(fn, NULL, NULL, NULL, NULL);
1223
1224         isst_ctdp_display_information_end(outf);
1225 }
1226
1227 static void adjust_scaling_max_from_base_freq(int cpu);
1228
1229 static void set_tdp_level_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1230                                   void *arg4)
1231 {
1232         int ret;
1233
1234         ret = isst_set_tdp_level(id, tdp_level);
1235         if (ret) {
1236                 isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1237                 isst_ctdp_display_information_end(outf);
1238                 exit(1);
1239         } else {
1240                 isst_display_result(id, outf, "perf-profile", "set_tdp_level",
1241                                     ret);
1242                 if (force_online_offline) {
1243                         struct isst_pkg_ctdp_level_info ctdp_level;
1244
1245                         /* Wait for updated base frequencies */
1246                         usleep(2000);
1247
1248                         /* Adjusting uncore freq */
1249                         isst_adjust_uncore_freq(id, tdp_level, &ctdp_level);
1250
1251                         fprintf(stderr, "Option is set to online/offline\n");
1252                         ctdp_level.core_cpumask_size =
1253                                 alloc_cpu_set(&ctdp_level.core_cpumask);
1254                         ret = isst_get_coremask_info(id, tdp_level, &ctdp_level);
1255                         if (ret) {
1256                                 isst_display_error_info_message(1, "Can't get coremask, online/offline option is ignored", 0, 0);
1257                                 return;
1258                         }
1259                         if (ctdp_level.cpu_count) {
1260                                 int i, max_cpus = get_topo_max_cpus();
1261                                 for (i = 0; i < max_cpus; ++i) {
1262                                         if (!is_cpu_in_power_domain(i, id))
1263                                                 continue;
1264                                         if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
1265                                                 fprintf(stderr, "online cpu %d\n", i);
1266                                                 set_cpu_online_offline(i, 1);
1267                                                 adjust_scaling_max_from_base_freq(i);
1268                                         } else {
1269                                                 fprintf(stderr, "offline cpu %d\n", i);
1270                                                 set_cpu_online_offline(i, 0);
1271                                         }
1272                                 }
1273                         }
1274                 }
1275         }
1276 }
1277
1278 static void set_tdp_level(int arg)
1279 {
1280         if (cmd_help) {
1281                 fprintf(stderr, "Set Config TDP level\n");
1282                 fprintf(stderr,
1283                         "\t Arguments: -l|--level : Specify tdp level\n");
1284                 fprintf(stderr,
1285                         "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1286                 fprintf(stderr,
1287                         "\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
1288                 exit(0);
1289         }
1290
1291         if (tdp_level == 0xff) {
1292                 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1293                 exit(1);
1294         }
1295         isst_ctdp_display_information_start(outf);
1296         if (max_target_cpus)
1297                 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
1298                                                   NULL, NULL, NULL);
1299         else
1300                 for_each_online_power_domain_in_set(set_tdp_level_for_cpu, NULL,
1301                                                NULL, NULL, NULL);
1302         isst_ctdp_display_information_end(outf);
1303 }
1304
1305 static void clx_n_dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1306                                        void *arg3, void *arg4)
1307 {
1308         int ret;
1309
1310         ret = clx_n_config(id);
1311         if (ret) {
1312                 isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
1313         } else {
1314                 struct isst_pkg_ctdp_level_info *ctdp_level;
1315                 struct isst_pbf_info *pbf_info;
1316
1317                 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1318                 pbf_info = &ctdp_level->pbf_info;
1319                 isst_pbf_display_information(id, outf, tdp_level, pbf_info);
1320                 free_cpu_set(ctdp_level->core_cpumask);
1321                 free_cpu_set(pbf_info->core_cpumask);
1322         }
1323 }
1324
1325 static void dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1326                                     void *arg4)
1327 {
1328         struct isst_pbf_info pbf_info;
1329         int ret;
1330
1331         ret = isst_get_pbf_info(id, tdp_level, &pbf_info);
1332         if (ret) {
1333                 isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
1334                 isst_ctdp_display_information_end(outf);
1335                 exit(1);
1336         } else {
1337                 isst_pbf_display_information(id, outf, tdp_level, &pbf_info);
1338                 free_cpu_set(pbf_info.core_cpumask);
1339         }
1340 }
1341
1342 static void dump_pbf_config(int arg)
1343 {
1344         void *fn;
1345
1346         if (cmd_help) {
1347                 fprintf(stderr,
1348                         "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
1349                 fprintf(stderr,
1350                         "\tArguments: -l|--level : Specify tdp level\n");
1351                 exit(0);
1352         }
1353
1354         if (tdp_level == 0xff) {
1355                 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1356                 exit(1);
1357         }
1358
1359         if (!is_clx_n_platform())
1360                 fn = dump_pbf_config_for_cpu;
1361         else
1362                 fn = clx_n_dump_pbf_config_for_cpu;
1363
1364         isst_ctdp_display_information_start(outf);
1365
1366         if (max_target_cpus)
1367                 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1368         else
1369                 for_each_online_power_domain_in_set(fn, NULL, NULL, NULL, NULL);
1370
1371         isst_ctdp_display_information_end(outf);
1372 }
1373
1374 static int set_clos_param(struct isst_id *id, int clos, int epp, int wt, int min, int max)
1375 {
1376         struct isst_clos_config clos_config;
1377         int ret;
1378
1379         ret = isst_pm_get_clos(id, clos, &clos_config);
1380         if (ret) {
1381                 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1382                 return ret;
1383         }
1384         clos_config.clos_min = min;
1385         clos_config.clos_max = max;
1386         clos_config.epp = epp;
1387         clos_config.clos_prop_prio = wt;
1388         ret = isst_set_clos(id, clos, &clos_config);
1389         if (ret) {
1390                 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1391                 return ret;
1392         }
1393
1394         return 0;
1395 }
1396
1397 static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1398 {
1399         char buffer[128], freq_str[16];
1400         int fd, ret, len;
1401
1402         if (max)
1403                 snprintf(buffer, sizeof(buffer),
1404                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1405         else
1406                 snprintf(buffer, sizeof(buffer),
1407                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1408
1409         fd = open(buffer, O_WRONLY);
1410         if (fd < 0)
1411                 return fd;
1412
1413         snprintf(freq_str, sizeof(freq_str), "%d", freq);
1414         len = strlen(freq_str);
1415         ret = write(fd, freq_str, len);
1416         if (ret == -1) {
1417                 close(fd);
1418                 return ret;
1419         }
1420         close(fd);
1421
1422         return 0;
1423 }
1424
1425 static int no_turbo(void)
1426 {
1427         return parse_int_file(0, "/sys/devices/system/cpu/intel_pstate/no_turbo");
1428 }
1429
1430 static void adjust_scaling_max_from_base_freq(int cpu)
1431 {
1432         int base_freq, scaling_max_freq;
1433
1434         scaling_max_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1435         base_freq = get_cpufreq_base_freq(cpu);
1436         if (scaling_max_freq < base_freq || no_turbo())
1437                 set_cpufreq_scaling_min_max(cpu, 1, base_freq);
1438 }
1439
1440 static void adjust_scaling_min_from_base_freq(int cpu)
1441 {
1442         int base_freq, scaling_min_freq;
1443
1444         scaling_min_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1445         base_freq = get_cpufreq_base_freq(cpu);
1446         if (scaling_min_freq < base_freq)
1447                 set_cpufreq_scaling_min_max(cpu, 0, base_freq);
1448 }
1449
1450 static int set_clx_pbf_cpufreq_scaling_min_max(struct isst_id *id)
1451 {
1452         struct isst_pkg_ctdp_level_info *ctdp_level;
1453         struct isst_pbf_info *pbf_info;
1454         int i, freq, freq_high, freq_low;
1455         int ret;
1456
1457         ret = clx_n_config(id);
1458         if (ret) {
1459                 debug_printf("cpufreq_scaling_min_max failed for CLX");
1460                 return ret;
1461         }
1462
1463         ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1464         pbf_info = &ctdp_level->pbf_info;
1465         freq_high = pbf_info->p1_high * 100000;
1466         freq_low = pbf_info->p1_low * 100000;
1467
1468         for (i = 0; i < get_topo_max_cpus(); ++i) {
1469                 if (!is_cpu_in_power_domain(i, id))
1470                         continue;
1471
1472                 if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1473                                   pbf_info->core_cpumask))
1474                         freq = freq_high;
1475                 else
1476                         freq = freq_low;
1477
1478                 set_cpufreq_scaling_min_max(i, 1, freq);
1479                 set_cpufreq_scaling_min_max(i, 0, freq);
1480         }
1481
1482         return 0;
1483 }
1484
1485 static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1486 {
1487         char buffer[128], min_freq[16];
1488         int fd, ret, len;
1489
1490         if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1491                 return -1;
1492
1493         if (cpuinfo_max)
1494                 snprintf(buffer, sizeof(buffer),
1495                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1496         else
1497                 snprintf(buffer, sizeof(buffer),
1498                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1499
1500         fd = open(buffer, O_RDONLY);
1501         if (fd < 0)
1502                 return fd;
1503
1504         len = read(fd, min_freq, sizeof(min_freq));
1505         close(fd);
1506
1507         if (len < 0)
1508                 return len;
1509
1510         if (scaling_max)
1511                 snprintf(buffer, sizeof(buffer),
1512                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1513         else
1514                 snprintf(buffer, sizeof(buffer),
1515                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1516
1517         fd = open(buffer, O_WRONLY);
1518         if (fd < 0)
1519                 return fd;
1520
1521         min_freq[15] = '\0';
1522         len = strlen(min_freq);
1523         ret = write(fd, min_freq, len);
1524         if (ret == -1) {
1525                 close(fd);
1526                 return ret;
1527         }
1528         close(fd);
1529
1530         return 0;
1531 }
1532
1533 static void set_scaling_min_to_cpuinfo_max(struct isst_id *id)
1534 {
1535         int i;
1536
1537         for (i = 0; i < get_topo_max_cpus(); ++i) {
1538                 if (!is_cpu_in_power_domain(i, id))
1539                         continue;
1540
1541                 if (is_cpu_online(i) != 1)
1542                         continue;
1543
1544                 adjust_scaling_max_from_base_freq(i);
1545                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1546                 adjust_scaling_min_from_base_freq(i);
1547         }
1548 }
1549
1550 static void set_scaling_min_to_cpuinfo_min(struct isst_id *id)
1551 {
1552         int i;
1553
1554         for (i = 0; i < get_topo_max_cpus(); ++i) {
1555                 if (!is_cpu_in_power_domain(i, id))
1556                         continue;
1557
1558                 if (is_cpu_online(i) != 1)
1559                         continue;
1560
1561                 adjust_scaling_max_from_base_freq(i);
1562                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1563         }
1564 }
1565
1566 static void set_scaling_max_to_cpuinfo_max(struct isst_id *id)
1567 {
1568         int i;
1569
1570         for (i = 0; i < get_topo_max_cpus(); ++i) {
1571                 if (!is_cpu_in_power_domain(i, id))
1572                         continue;
1573
1574                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1575         }
1576 }
1577
1578 static int set_core_priority_and_min(struct isst_id *id, int mask_size,
1579                                      cpu_set_t *cpu_mask, int min_high,
1580                                      int min_low)
1581 {
1582         int ret, i;
1583
1584         if (!CPU_COUNT_S(mask_size, cpu_mask))
1585                 return -1;
1586
1587         ret = set_clos_param(id, 0, 0, 0, min_high, 0xff);
1588         if (ret)
1589                 return ret;
1590
1591         ret = set_clos_param(id, 1, 15, 15, min_low, 0xff);
1592         if (ret)
1593                 return ret;
1594
1595         ret = set_clos_param(id, 2, 15, 15, min_low, 0xff);
1596         if (ret)
1597                 return ret;
1598
1599         ret = set_clos_param(id, 3, 15, 15, min_low, 0xff);
1600         if (ret)
1601                 return ret;
1602
1603         for (i = 0; i < get_topo_max_cpus(); ++i) {
1604                 int clos;
1605                 struct isst_id tid;
1606
1607                 if (!is_cpu_in_power_domain(i, id))
1608                         continue;
1609
1610                 if (CPU_ISSET_S(i, mask_size, cpu_mask))
1611                         clos = 0;
1612                 else
1613                         clos = 3;
1614
1615                 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1616                 set_isst_id(&tid, i);
1617                 ret = isst_clos_associate(&tid, clos);
1618                 if (ret) {
1619                         isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1620                         return ret;
1621                 }
1622         }
1623
1624         return 0;
1625 }
1626
1627 static int set_pbf_core_power(struct isst_id *id)
1628 {
1629         struct isst_pbf_info pbf_info;
1630         struct isst_pkg_ctdp pkg_dev;
1631         int ret;
1632
1633         ret = isst_get_ctdp_levels(id, &pkg_dev);
1634         if (ret) {
1635                 debug_printf("isst_get_ctdp_levels failed");
1636                 return ret;
1637         }
1638         debug_printf("Current_level: %d\n", pkg_dev.current_level);
1639
1640         ret = isst_get_pbf_info(id, pkg_dev.current_level, &pbf_info);
1641         if (ret) {
1642                 debug_printf("isst_get_pbf_info failed");
1643                 return ret;
1644         }
1645         debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1646                      pbf_info.p1_low);
1647
1648         ret = set_core_priority_and_min(id, pbf_info.core_cpumask_size,
1649                                         pbf_info.core_cpumask,
1650                                         pbf_info.p1_high, pbf_info.p1_low);
1651         if (ret) {
1652                 debug_printf("set_core_priority_and_min failed");
1653                 return ret;
1654         }
1655
1656         ret = isst_pm_qos_config(id, 1, 1);
1657         if (ret) {
1658                 debug_printf("isst_pm_qos_config failed");
1659                 return ret;
1660         }
1661
1662         return 0;
1663 }
1664
1665 static void set_pbf_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1666                             void *arg4)
1667 {
1668         struct isst_pkg_ctdp_level_info ctdp_level;
1669         struct isst_pkg_ctdp pkg_dev;
1670         int ret;
1671         int status = *(int *)arg4;
1672
1673         if (is_clx_n_platform()) {
1674                 ret = 0;
1675                 if (status) {
1676                         set_clx_pbf_cpufreq_scaling_min_max(id);
1677
1678                 } else {
1679                         set_scaling_max_to_cpuinfo_max(id);
1680                         set_scaling_min_to_cpuinfo_min(id);
1681                 }
1682                 goto disp_result;
1683         }
1684
1685         ret = isst_get_ctdp_levels(id, &pkg_dev);
1686         if (ret) {
1687                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
1688                 goto disp_result;
1689         }
1690
1691         ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
1692         if (ret) {
1693                 isst_display_error_info_message(1, "Failed to get current level", 0, 0);
1694                 goto disp_result;
1695         }
1696
1697         if (!ctdp_level.pbf_support) {
1698                 isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, pkg_dev.current_level);
1699                 ret = -1;
1700                 goto disp_result;
1701         }
1702
1703         if (auto_mode && status) {
1704                 ret = set_pbf_core_power(id);
1705                 if (ret)
1706                         goto disp_result;
1707         }
1708
1709         ret = isst_set_pbf_fact_status(id, 1, status);
1710         if (ret) {
1711                 debug_printf("isst_set_pbf_fact_status failed");
1712                 if (auto_mode)
1713                         isst_pm_qos_config(id, 0, 0);
1714         } else {
1715                 if (auto_mode) {
1716                         if (status)
1717                                 set_scaling_min_to_cpuinfo_max(id);
1718                         else
1719                                 set_scaling_min_to_cpuinfo_min(id);
1720                 }
1721         }
1722
1723         if (auto_mode && !status)
1724                 isst_pm_qos_config(id, 0, 1);
1725
1726 disp_result:
1727         if (status)
1728                 isst_display_result(id, outf, "base-freq", "enable",
1729                                     ret);
1730         else
1731                 isst_display_result(id, outf, "base-freq", "disable",
1732                                     ret);
1733 }
1734
1735 static void set_pbf_enable(int arg)
1736 {
1737         int enable = arg;
1738
1739         if (cmd_help) {
1740                 if (enable) {
1741                         fprintf(stderr,
1742                                 "Enable Intel Speed Select Technology base frequency feature\n");
1743                         if (is_clx_n_platform()) {
1744                                 fprintf(stderr,
1745                                         "\tOn this platform this command doesn't enable feature in the hardware.\n");
1746                                 fprintf(stderr,
1747                                         "\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
1748                                 exit(0);
1749
1750                         }
1751                         fprintf(stderr,
1752                                 "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1753                 } else {
1754
1755                         if (is_clx_n_platform()) {
1756                                 fprintf(stderr,
1757                                         "\tOn this platform this command doesn't disable feature in the hardware.\n");
1758                                 fprintf(stderr,
1759                                         "\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
1760                                 exit(0);
1761                         }
1762                         fprintf(stderr,
1763                                 "Disable Intel Speed Select Technology base frequency feature\n");
1764                         fprintf(stderr,
1765                                 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1766                 }
1767                 exit(0);
1768         }
1769
1770         isst_ctdp_display_information_start(outf);
1771         if (max_target_cpus)
1772                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1773                                                   NULL, &enable);
1774         else
1775                 for_each_online_power_domain_in_set(set_pbf_for_cpu, NULL, NULL,
1776                                                NULL, &enable);
1777         isst_ctdp_display_information_end(outf);
1778 }
1779
1780 static void dump_fact_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1781                                      void *arg3, void *arg4)
1782 {
1783         struct isst_fact_info fact_info;
1784         int ret;
1785
1786         ret = isst_get_fact_info(id, tdp_level, fact_bucket, &fact_info);
1787         if (ret) {
1788                 isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1789                 isst_ctdp_display_information_end(outf);
1790                 exit(1);
1791         } else {
1792                 isst_fact_display_information(id, outf, tdp_level, fact_bucket,
1793                                               fact_avx, &fact_info);
1794         }
1795 }
1796
1797 static void dump_fact_config(int arg)
1798 {
1799         if (cmd_help) {
1800                 fprintf(stderr,
1801                         "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
1802                 fprintf(stderr,
1803                         "\tArguments: -l|--level : Specify tdp level\n");
1804                 fprintf(stderr,
1805                         "\tArguments: -b|--bucket : Bucket index to dump\n");
1806                 fprintf(stderr,
1807                         "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
1808                 exit(0);
1809         }
1810
1811         if (tdp_level == 0xff) {
1812                 isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
1813                 exit(1);
1814         }
1815
1816         isst_ctdp_display_information_start(outf);
1817         if (max_target_cpus)
1818                 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
1819                                                   NULL, NULL, NULL, NULL);
1820         else
1821                 for_each_online_power_domain_in_set(dump_fact_config_for_cpu, NULL,
1822                                                NULL, NULL, NULL);
1823         isst_ctdp_display_information_end(outf);
1824 }
1825
1826 static void set_fact_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
1827                              void *arg4)
1828 {
1829         struct isst_pkg_ctdp_level_info ctdp_level;
1830         struct isst_pkg_ctdp pkg_dev;
1831         int ret;
1832         int status = *(int *)arg4;
1833
1834         if (status && no_turbo()) {
1835                 isst_display_error_info_message(1, "Turbo mode is disabled", 0, 0);
1836                 ret = -1;
1837                 goto disp_results;
1838         }
1839
1840         ret = isst_get_ctdp_levels(id, &pkg_dev);
1841         if (ret) {
1842                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
1843                 goto disp_results;
1844         }
1845
1846         ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
1847         if (ret) {
1848                 isst_display_error_info_message(1, "Failed to get current level", 0, 0);
1849                 goto disp_results;
1850         }
1851
1852         if (!ctdp_level.fact_support) {
1853                 isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, pkg_dev.current_level);
1854                 ret = -1;
1855                 goto disp_results;
1856         }
1857
1858         if (status) {
1859                 ret = isst_pm_qos_config(id, 1, 1);
1860                 if (ret)
1861                         goto disp_results;
1862         }
1863
1864         ret = isst_set_pbf_fact_status(id, 0, status);
1865         if (ret) {
1866                 debug_printf("isst_set_pbf_fact_status failed");
1867                 if (auto_mode)
1868                         isst_pm_qos_config(id, 0, 0);
1869
1870                 goto disp_results;
1871         }
1872
1873         /* Set TRL */
1874         if (status) {
1875                 struct isst_pkg_ctdp pkg_dev;
1876
1877                 ret = isst_get_ctdp_levels(id, &pkg_dev);
1878                 if (!ret)
1879                         ret = isst_set_trl(id, fact_trl);
1880                 if (ret && auto_mode)
1881                         isst_pm_qos_config(id, 0, 0);
1882         } else {
1883                 if (auto_mode)
1884                         isst_pm_qos_config(id, 0, 0);
1885         }
1886
1887 disp_results:
1888         if (status) {
1889                 isst_display_result(id, outf, "turbo-freq", "enable", ret);
1890                 if (ret)
1891                         fact_enable_fail = ret;
1892         } else {
1893                 /* Since we modified TRL during Fact enable, restore it */
1894                 isst_set_trl_from_current_tdp(id, fact_trl);
1895                 isst_display_result(id, outf, "turbo-freq", "disable", ret);
1896         }
1897 }
1898
1899 static void set_fact_enable(int arg)
1900 {
1901         int i, ret, enable = arg;
1902         struct isst_id id;
1903
1904         if (cmd_help) {
1905                 if (enable) {
1906                         fprintf(stderr,
1907                                 "Enable Intel Speed Select Technology Turbo frequency feature\n");
1908                         fprintf(stderr,
1909                                 "Optional: -t|--trl : Specify turbo ratio limit\n");
1910                         fprintf(stderr,
1911                                 "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1912                         fprintf(stderr,
1913                                 "-C|--cpu option as as high priority using core-power feature\n");
1914                 } else {
1915                         fprintf(stderr,
1916                                 "Disable Intel Speed Select Technology turbo frequency feature\n");
1917                         fprintf(stderr,
1918                                 "Optional: -t|--trl : Specify turbo ratio limit\n");
1919                         fprintf(stderr,
1920                                 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1921                 }
1922                 exit(0);
1923         }
1924
1925         isst_ctdp_display_information_start(outf);
1926         if (max_target_cpus)
1927                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1928                                                   NULL, &enable);
1929         else
1930                 for_each_online_power_domain_in_set(set_fact_for_cpu, NULL, NULL,
1931                                                NULL, &enable);
1932         isst_ctdp_display_information_end(outf);
1933
1934         if (!fact_enable_fail && enable && auto_mode) {
1935                 /*
1936                  * When we adjust CLOS param, we have to set for siblings also.
1937                  * So for the each user specified CPU, also add the sibling
1938                  * in the present_cpu_mask.
1939                  */
1940                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1941                         char buffer[128], sibling_list[128], *cpu_str;
1942                         int fd, len;
1943
1944                         if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1945                                 continue;
1946
1947                         snprintf(buffer, sizeof(buffer),
1948                                  "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1949
1950                         fd = open(buffer, O_RDONLY);
1951                         if (fd < 0)
1952                                 continue;
1953
1954                         len = read(fd, sibling_list, sizeof(sibling_list));
1955                         close(fd);
1956
1957                         if (len < 0)
1958                                 continue;
1959
1960                         sibling_list[127] = '\0';
1961                         cpu_str = strtok(sibling_list, ",");
1962                         while (cpu_str != NULL) {
1963                                 int cpu;
1964
1965                                 sscanf(cpu_str, "%d", &cpu);
1966                                 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1967                                 cpu_str = strtok(NULL, ",");
1968                         }
1969                 }
1970
1971                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1972                         int clos;
1973
1974                         if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1975                                 continue;
1976
1977                         if (is_cpu_online(i) != 1)
1978                                 continue;
1979
1980                         set_isst_id(&id, i);
1981                         ret = set_clos_param(&id, 0, 0, 0, 0, 0xff);
1982                         if (ret)
1983                                 goto error_disp;
1984
1985                         ret = set_clos_param(&id, 1, 15, 15, 0, 0xff);
1986                         if (ret)
1987                                 goto error_disp;
1988
1989                         ret = set_clos_param(&id, 2, 15, 15, 0, 0xff);
1990                         if (ret)
1991                                 goto error_disp;
1992
1993                         ret = set_clos_param(&id, 3, 15, 15, 0, 0xff);
1994                         if (ret)
1995                                 goto error_disp;
1996
1997                         if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1998                                 clos = 0;
1999                         else
2000                                 clos = 3;
2001
2002                         debug_printf("Associate cpu: %d clos: %d\n", i, clos);
2003                         ret = isst_clos_associate(&id, clos);
2004                         if (ret)
2005                                 goto error_disp;
2006                 }
2007                 set_isst_id(&id, -1);
2008                 isst_display_result(&id, outf, "turbo-freq --auto", "enable", 0);
2009         }
2010
2011         return;
2012
2013 error_disp:
2014         isst_display_result(&id, outf, "turbo-freq --auto", "enable", ret);
2015
2016 }
2017
2018 static void enable_clos_qos_config(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2019                                    void *arg4)
2020 {
2021         int ret;
2022         int status = *(int *)arg4;
2023
2024         if (is_skx_based_platform())
2025                 clos_priority_type = 1;
2026
2027         ret = isst_pm_qos_config(id, status, clos_priority_type);
2028         if (ret)
2029                 isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
2030
2031         if (status)
2032                 isst_display_result(id, outf, "core-power", "enable",
2033                                     ret);
2034         else
2035                 isst_display_result(id, outf, "core-power", "disable",
2036                                     ret);
2037 }
2038
2039 static void set_clos_enable(int arg)
2040 {
2041         int enable = arg;
2042
2043         if (cmd_help) {
2044                 if (enable) {
2045                         fprintf(stderr,
2046                                 "Enable core-power for a package/die\n");
2047                         if (!is_skx_based_platform()) {
2048                                 fprintf(stderr,
2049                                         "\tClos Enable: Specify priority type with [--priority|-p]\n");
2050                                 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
2051                         }
2052                 } else {
2053                         fprintf(stderr,
2054                                 "Disable core-power: [No command arguments are required]\n");
2055                 }
2056                 exit(0);
2057         }
2058
2059         if (enable && cpufreq_sysfs_present()) {
2060                 fprintf(stderr,
2061                         "cpufreq subsystem and core-power enable will interfere with each other!\n");
2062         }
2063
2064         isst_ctdp_display_information_start(outf);
2065         if (max_target_cpus)
2066                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
2067                                                   NULL, NULL, &enable);
2068         else
2069                 for_each_online_power_domain_in_set(enable_clos_qos_config, NULL,
2070                                                NULL, NULL, &enable);
2071         isst_ctdp_display_information_end(outf);
2072 }
2073
2074 static void dump_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
2075                                      void *arg3, void *arg4)
2076 {
2077         struct isst_clos_config clos_config;
2078         int ret;
2079
2080         ret = isst_pm_get_clos(id, current_clos, &clos_config);
2081         if (ret)
2082                 isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
2083         else
2084                 isst_clos_display_information(id, outf, current_clos,
2085                                               &clos_config);
2086 }
2087
2088 static void dump_clos_config(int arg)
2089 {
2090         if (cmd_help) {
2091                 fprintf(stderr,
2092                         "Print Intel Speed Select Technology core power configuration\n");
2093                 fprintf(stderr,
2094                         "\tArguments: [-c | --clos]: Specify clos id\n");
2095                 exit(0);
2096         }
2097         if (current_clos < 0 || current_clos > 3) {
2098                 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2099                 isst_ctdp_display_information_end(outf);
2100                 exit(0);
2101         }
2102
2103         isst_ctdp_display_information_start(outf);
2104         if (max_target_cpus)
2105                 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
2106                                                   NULL, NULL, NULL, NULL);
2107         else
2108                 for_each_online_power_domain_in_set(dump_clos_config_for_cpu, NULL,
2109                                                NULL, NULL, NULL);
2110         isst_ctdp_display_information_end(outf);
2111 }
2112
2113 static void get_clos_info_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2114                                   void *arg4)
2115 {
2116         int enable, ret, prio_type;
2117
2118         ret = isst_clos_get_clos_information(id, &enable, &prio_type);
2119         if (ret)
2120                 isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2121         else {
2122                 int cp_state, cp_cap;
2123
2124                 isst_read_pm_config(id, &cp_state, &cp_cap);
2125                 isst_clos_display_clos_information(id, outf, enable, prio_type,
2126                                                    cp_state, cp_cap);
2127         }
2128 }
2129
2130 static void dump_clos_info(int arg)
2131 {
2132         if (cmd_help) {
2133                 fprintf(stderr,
2134                         "Print Intel Speed Select Technology core power information\n");
2135                 fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2136                 exit(0);
2137         }
2138
2139         isst_ctdp_display_information_start(outf);
2140         if (max_target_cpus)
2141                 for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2142                                                   NULL, NULL, NULL);
2143         else
2144                 for_each_online_power_domain_in_set(get_clos_info_for_cpu, NULL,
2145                                                NULL, NULL, NULL);
2146         isst_ctdp_display_information_end(outf);
2147
2148 }
2149
2150 static void set_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2151                                     void *arg4)
2152 {
2153         struct isst_clos_config clos_config;
2154         int ret;
2155
2156         clos_config.epp = clos_epp;
2157         clos_config.clos_prop_prio = clos_prop_prio;
2158         clos_config.clos_min = clos_min;
2159         clos_config.clos_max = clos_max;
2160         clos_config.clos_desired = clos_desired;
2161         ret = isst_set_clos(id, current_clos, &clos_config);
2162         if (ret)
2163                 isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
2164         else
2165                 isst_display_result(id, outf, "core-power", "config", ret);
2166 }
2167
2168 static void set_clos_config(int arg)
2169 {
2170         if (cmd_help) {
2171                 fprintf(stderr,
2172                         "Set core-power configuration for one of the four clos ids\n");
2173                 fprintf(stderr,
2174                         "\tSpecify targeted clos id with [--clos|-c]\n");
2175                 if (!is_skx_based_platform()) {
2176                         fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
2177                         fprintf(stderr,
2178                                 "\tSpecify clos Proportional Priority [--weight|-w]\n");
2179                 }
2180                 fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
2181                 fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
2182                 exit(0);
2183         }
2184
2185         if (current_clos < 0 || current_clos > 3) {
2186                 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2187                 exit(0);
2188         }
2189         if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2190                 fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
2191                 clos_epp = 0;
2192         }
2193         if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
2194                 fprintf(stderr,
2195                         "clos frequency weight is not specified or invalid, default: 0\n");
2196                 clos_prop_prio = 0;
2197         }
2198         if (clos_min < 0) {
2199                 fprintf(stderr, "clos min is not specified, default: 0\n");
2200                 clos_min = 0;
2201         }
2202         if (clos_max < 0) {
2203                 fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
2204                 clos_max = 0xff;
2205         }
2206         if (clos_desired) {
2207                 fprintf(stderr, "clos desired is not supported on this platform\n");
2208                 clos_desired = 0x00;
2209         }
2210
2211         isst_ctdp_display_information_start(outf);
2212         if (max_target_cpus)
2213                 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
2214                                                   NULL, NULL, NULL);
2215         else
2216                 for_each_online_power_domain_in_set(set_clos_config_for_cpu, NULL,
2217                                                NULL, NULL, NULL);
2218         isst_ctdp_display_information_end(outf);
2219 }
2220
2221 static void set_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2222                                    void *arg4)
2223 {
2224         int ret;
2225
2226         ret = isst_clos_associate(id, current_clos);
2227         if (ret)
2228                 debug_printf("isst_clos_associate failed");
2229         else
2230                 isst_display_result(id, outf, "core-power", "assoc", ret);
2231 }
2232
2233 static void set_clos_assoc(int arg)
2234 {
2235         if (cmd_help) {
2236                 fprintf(stderr, "Associate a clos id to a CPU\n");
2237                 fprintf(stderr,
2238                         "\tSpecify targeted clos id with [--clos|-c]\n");
2239                 fprintf(stderr,
2240                         "\tFor example to associate clos 1 to CPU 0: issue\n");
2241                 fprintf(stderr,
2242                         "\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
2243                 exit(0);
2244         }
2245
2246         if (current_clos < 0 || current_clos > 3) {
2247                 isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2248                 exit(0);
2249         }
2250         if (max_target_cpus)
2251                 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
2252                                                   NULL, NULL, NULL);
2253         else {
2254                 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2255         }
2256 }
2257
2258 static void get_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2259                                    void *arg4)
2260 {
2261         int clos, ret;
2262
2263         ret = isst_clos_get_assoc_status(id, &clos);
2264         if (ret)
2265                 isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
2266         else
2267                 isst_clos_display_assoc_information(id, outf, clos);
2268 }
2269
2270 static void get_clos_assoc(int arg)
2271 {
2272         if (cmd_help) {
2273                 fprintf(stderr, "Get associate clos id to a CPU\n");
2274                 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
2275                 exit(0);
2276         }
2277
2278         if (!max_target_cpus) {
2279                 isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2280                 exit(0);
2281         }
2282
2283         isst_ctdp_display_information_start(outf);
2284         for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2285                                           NULL, NULL, NULL);
2286         isst_ctdp_display_information_end(outf);
2287 }
2288
2289 static void set_turbo_mode_for_cpu(struct isst_id *id, int status)
2290 {
2291         int base_freq;
2292
2293         if (status) {
2294                 base_freq = get_cpufreq_base_freq(id->cpu);
2295                 set_cpufreq_scaling_min_max(id->cpu, 1, base_freq);
2296         } else {
2297                 set_scaling_max_to_cpuinfo_max(id);
2298         }
2299
2300         if (status) {
2301                 isst_display_result(id, outf, "turbo-mode", "enable", 0);
2302         } else {
2303                 isst_display_result(id, outf, "turbo-mode", "disable", 0);
2304         }
2305 }
2306
2307 static void set_turbo_mode(int arg)
2308 {
2309         int i, enable = arg;
2310         struct isst_id id;
2311
2312         if (cmd_help) {
2313                 if (enable)
2314                         fprintf(stderr, "Set turbo mode enable\n");
2315                 else
2316                         fprintf(stderr, "Set turbo mode disable\n");
2317                 exit(0);
2318         }
2319
2320         isst_ctdp_display_information_start(outf);
2321
2322         for (i = 0; i < topo_max_cpus; ++i) {
2323                 int online;
2324
2325                 if (i)
2326                         online = parse_int_file(
2327                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
2328                 else
2329                         online =
2330                                 1; /* online entry for CPU 0 needs some special configs */
2331
2332                 if (online) {
2333                         set_isst_id(&id, i);
2334                         set_turbo_mode_for_cpu(&id, enable);
2335                 }
2336
2337         }
2338         isst_ctdp_display_information_end(outf);
2339 }
2340
2341 static void get_set_trl(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2342                         void *arg4)
2343 {
2344         unsigned long long trl;
2345         int set = *(int *)arg4;
2346         int ret;
2347
2348         if (set && !fact_trl) {
2349                 isst_display_error_info_message(1, "Invalid TRL. Specify with [-t|--trl]", 0, 0);
2350                 exit(0);
2351         }
2352
2353         if (set) {
2354                 ret = isst_set_trl(id, fact_trl);
2355                 isst_display_result(id, outf, "turbo-mode", "set-trl", ret);
2356                 return;
2357         }
2358
2359         ret = isst_get_trl(id, &trl);
2360         if (ret)
2361                 isst_display_result(id, outf, "turbo-mode", "get-trl", ret);
2362         else
2363                 isst_trl_display_information(id, outf, trl);
2364 }
2365
2366 static void process_trl(int arg)
2367 {
2368         if (cmd_help) {
2369                 if (arg) {
2370                         fprintf(stderr, "Set TRL (turbo ratio limits)\n");
2371                         fprintf(stderr, "\t t|--trl: Specify turbo ratio limit for setting TRL\n");
2372                 } else {
2373                         fprintf(stderr, "Get TRL (turbo ratio limits)\n");
2374                 }
2375                 exit(0);
2376         }
2377
2378         isst_ctdp_display_information_start(outf);
2379         if (max_target_cpus)
2380                 for_each_online_target_cpu_in_set(get_set_trl, NULL,
2381                                                   NULL, NULL, &arg);
2382         else
2383                 for_each_online_power_domain_in_set(get_set_trl, NULL,
2384                                                NULL, NULL, &arg);
2385         isst_ctdp_display_information_end(outf);
2386 }
2387
2388 static struct process_cmd_struct clx_n_cmds[] = {
2389         { "perf-profile", "info", dump_isst_config, 0 },
2390         { "base-freq", "info", dump_pbf_config, 0 },
2391         { "base-freq", "enable", set_pbf_enable, 1 },
2392         { "base-freq", "disable", set_pbf_enable, 0 },
2393         { NULL, NULL, NULL, 0 }
2394 };
2395
2396 static struct process_cmd_struct isst_cmds[] = {
2397         { "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2398         { "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2399         { "perf-profile", "get-config-version", get_tdp_version, 0 },
2400         { "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2401         { "perf-profile", "get-config-current-level", get_tdp_current_level,
2402          0 },
2403         { "perf-profile", "set-config-level", set_tdp_level, 0 },
2404         { "perf-profile", "info", dump_isst_config, 0 },
2405         { "base-freq", "info", dump_pbf_config, 0 },
2406         { "base-freq", "enable", set_pbf_enable, 1 },
2407         { "base-freq", "disable", set_pbf_enable, 0 },
2408         { "turbo-freq", "info", dump_fact_config, 0 },
2409         { "turbo-freq", "enable", set_fact_enable, 1 },
2410         { "turbo-freq", "disable", set_fact_enable, 0 },
2411         { "core-power", "info", dump_clos_info, 0 },
2412         { "core-power", "enable", set_clos_enable, 1 },
2413         { "core-power", "disable", set_clos_enable, 0 },
2414         { "core-power", "config", set_clos_config, 0 },
2415         { "core-power", "get-config", dump_clos_config, 0 },
2416         { "core-power", "assoc", set_clos_assoc, 0 },
2417         { "core-power", "get-assoc", get_clos_assoc, 0 },
2418         { "turbo-mode", "enable", set_turbo_mode, 0 },
2419         { "turbo-mode", "disable", set_turbo_mode, 1 },
2420         { "turbo-mode", "get-trl", process_trl, 0 },
2421         { "turbo-mode", "set-trl", process_trl, 1 },
2422         { NULL, NULL, NULL }
2423 };
2424
2425 /*
2426  * parse cpuset with following syntax
2427  * 1,2,4..6,8-10 and set bits in cpu_subset
2428  */
2429 void parse_cpu_command(char *optarg)
2430 {
2431         unsigned int start, end;
2432         char *next;
2433
2434         next = optarg;
2435
2436         while (next && *next) {
2437                 if (*next == '-') /* no negative cpu numbers */
2438                         goto error;
2439
2440                 start = strtoul(next, &next, 10);
2441
2442                 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2443                         target_cpus[max_target_cpus++] = start;
2444
2445                 if (*next == '\0')
2446                         break;
2447
2448                 if (*next == ',') {
2449                         next += 1;
2450                         continue;
2451                 }
2452
2453                 if (*next == '-') {
2454                         next += 1; /* start range */
2455                 } else if (*next == '.') {
2456                         next += 1;
2457                         if (*next == '.')
2458                                 next += 1; /* start range */
2459                         else
2460                                 goto error;
2461                 }
2462
2463                 end = strtoul(next, &next, 10);
2464                 if (end <= start)
2465                         goto error;
2466
2467                 while (++start <= end) {
2468                         if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2469                                 target_cpus[max_target_cpus++] = start;
2470                 }
2471
2472                 if (*next == ',')
2473                         next += 1;
2474                 else if (*next != '\0')
2475                         goto error;
2476         }
2477
2478 #ifdef DEBUG
2479         {
2480                 int i;
2481
2482                 for (i = 0; i < max_target_cpus; ++i)
2483                         printf("cpu [%d] in arg\n", target_cpus[i]);
2484         }
2485 #endif
2486         return;
2487
2488 error:
2489         fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
2490         exit(-1);
2491 }
2492
2493 static void parse_cmd_args(int argc, int start, char **argv)
2494 {
2495         int opt;
2496         int option_index;
2497
2498         static struct option long_options[] = {
2499                 { "bucket", required_argument, 0, 'b' },
2500                 { "level", required_argument, 0, 'l' },
2501                 { "online", required_argument, 0, 'o' },
2502                 { "trl-type", required_argument, 0, 'r' },
2503                 { "trl", required_argument, 0, 't' },
2504                 { "help", no_argument, 0, 'h' },
2505                 { "clos", required_argument, 0, 'c' },
2506                 { "desired", required_argument, 0, 'd' },
2507                 { "epp", required_argument, 0, 'e' },
2508                 { "min", required_argument, 0, 'n' },
2509                 { "max", required_argument, 0, 'm' },
2510                 { "priority", required_argument, 0, 'p' },
2511                 { "weight", required_argument, 0, 'w' },
2512                 { "auto", no_argument, 0, 'a' },
2513                 { 0, 0, 0, 0 }
2514         };
2515
2516         option_index = start;
2517
2518         optind = start + 1;
2519         while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
2520                                   long_options, &option_index)) != -1) {
2521                 switch (opt) {
2522                 case 'a':
2523                         auto_mode = 1;
2524                         break;
2525                 case 'b':
2526                         fact_bucket = atoi(optarg);
2527                         break;
2528                 case 'h':
2529                         cmd_help = 1;
2530                         break;
2531                 case 'l':
2532                         tdp_level = atoi(optarg);
2533                         break;
2534                 case 'o':
2535                         force_online_offline = 1;
2536                         break;
2537                 case 't':
2538                         sscanf(optarg, "0x%llx", &fact_trl);
2539                         break;
2540                 case 'r':
2541                         if (!strncmp(optarg, "sse", 3)) {
2542                                 fact_avx = 0x01;
2543                         } else if (!strncmp(optarg, "avx2", 4)) {
2544                                 fact_avx = 0x02;
2545                         } else if (!strncmp(optarg, "avx512", 6)) {
2546                                 fact_avx = 0x04;
2547                         } else {
2548                                 fprintf(outf, "Invalid sse,avx options\n");
2549                                 exit(1);
2550                         }
2551                         break;
2552                 /* CLOS related */
2553                 case 'c':
2554                         current_clos = atoi(optarg);
2555                         break;
2556                 case 'd':
2557                         clos_desired = atoi(optarg);
2558                         clos_desired /= isst_get_disp_freq_multiplier();
2559                         break;
2560                 case 'e':
2561                         clos_epp = atoi(optarg);
2562                         if (is_skx_based_platform()) {
2563                                 isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2564                                 exit(0);
2565                         }
2566                         break;
2567                 case 'n':
2568                         clos_min = atoi(optarg);
2569                         clos_min /= isst_get_disp_freq_multiplier();
2570                         break;
2571                 case 'm':
2572                         clos_max = atoi(optarg);
2573                         clos_max /= isst_get_disp_freq_multiplier();
2574                         break;
2575                 case 'p':
2576                         clos_priority_type = atoi(optarg);
2577                         if (is_skx_based_platform() && !clos_priority_type) {
2578                                 isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2579                                 exit(0);
2580                         }
2581                         break;
2582                 case 'w':
2583                         clos_prop_prio = atoi(optarg);
2584                         if (is_skx_based_platform()) {
2585                                 isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2586                                 exit(0);
2587                         }
2588                         break;
2589                 default:
2590                         printf("Unknown option: ignore\n");
2591                 }
2592         }
2593
2594         if (argv[optind])
2595                 printf("Garbage at the end of command: ignore\n");
2596 }
2597
2598 static void isst_help(void)
2599 {
2600         printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
2601                 performance profiles per system via static and/or dynamic\n\
2602                 adjustment of core count, workload, Tjmax, and\n\
2603                 TDP, etc.\n");
2604         printf("\nCommands : For feature=perf-profile\n");
2605         printf("\tinfo\n");
2606
2607         if (!is_clx_n_platform()) {
2608                 printf("\tget-lock-status\n");
2609                 printf("\tget-config-levels\n");
2610                 printf("\tget-config-version\n");
2611                 printf("\tget-config-enabled\n");
2612                 printf("\tget-config-current-level\n");
2613                 printf("\tset-config-level\n");
2614         }
2615 }
2616
2617 static void pbf_help(void)
2618 {
2619         printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
2620                 on certain cores (high priority cores) in exchange for lower\n\
2621                 base frequency on remaining cores (low priority cores).\n");
2622         printf("\tcommand : info\n");
2623         printf("\tcommand : enable\n");
2624         printf("\tcommand : disable\n");
2625 }
2626
2627 static void fact_help(void)
2628 {
2629         printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
2630                 limits to cores based on priority.\n");
2631         printf("\nCommand: For feature=turbo-freq\n");
2632         printf("\tcommand : info\n");
2633         printf("\tcommand : enable\n");
2634         printf("\tcommand : disable\n");
2635 }
2636
2637 static void turbo_mode_help(void)
2638 {
2639         printf("turbo-mode:\tEnables users to enable/disable turbo mode by adjusting frequency settings. Also allows to get and set turbo ratio limits (TRL).\n");
2640         printf("\tcommand : enable\n");
2641         printf("\tcommand : disable\n");
2642         printf("\tcommand : get-trl\n");
2643         printf("\tcommand : set-trl\n");
2644 }
2645
2646
2647 static void core_power_help(void)
2648 {
2649         printf("core-power:\tInterface that allows user to define per core/tile\n\
2650                 priority.\n");
2651         printf("\nCommands : For feature=core-power\n");
2652         printf("\tinfo\n");
2653         printf("\tenable\n");
2654         printf("\tdisable\n");
2655         printf("\tconfig\n");
2656         printf("\tget-config\n");
2657         printf("\tassoc\n");
2658         printf("\tget-assoc\n");
2659 }
2660
2661 struct process_cmd_help_struct {
2662         char *feature;
2663         void (*process_fn)(void);
2664 };
2665
2666 static struct process_cmd_help_struct isst_help_cmds[] = {
2667         { "perf-profile", isst_help },
2668         { "base-freq", pbf_help },
2669         { "turbo-freq", fact_help },
2670         { "core-power", core_power_help },
2671         { "turbo-mode", turbo_mode_help },
2672         { NULL, NULL }
2673 };
2674
2675 static struct process_cmd_help_struct clx_n_help_cmds[] = {
2676         { "perf-profile", isst_help },
2677         { "base-freq", pbf_help },
2678         { NULL, NULL }
2679 };
2680
2681 void process_command(int argc, char **argv,
2682                      struct process_cmd_help_struct *help_cmds,
2683                      struct process_cmd_struct *cmds)
2684 {
2685         int i = 0, matched = 0;
2686         char *feature = argv[optind];
2687         char *cmd = argv[optind + 1];
2688
2689         if (!feature || !cmd)
2690                 return;
2691
2692         debug_printf("feature name [%s] command [%s]\n", feature, cmd);
2693         if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2694                 while (help_cmds[i].feature) {
2695                         if (!strcmp(help_cmds[i].feature, feature)) {
2696                                 help_cmds[i].process_fn();
2697                                 exit(0);
2698                         }
2699                         ++i;
2700                 }
2701         }
2702
2703         i = 0;
2704         while (cmds[i].feature) {
2705                 if (!strcmp(cmds[i].feature, feature) &&
2706                     !strcmp(cmds[i].command, cmd)) {
2707                         parse_cmd_args(argc, optind + 1, argv);
2708                         cmds[i].process_fn(cmds[i].arg);
2709                         matched = 1;
2710                         break;
2711                 }
2712                 ++i;
2713         }
2714
2715         if (!matched)
2716                 fprintf(stderr, "Invalid command\n");
2717 }
2718
2719 static void usage(void)
2720 {
2721         if (is_clx_n_platform()) {
2722                 fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2723                 fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2724         }
2725
2726         printf("\nUsage:\n");
2727         printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2728         printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2729         if (is_clx_n_platform())
2730                 printf("\nFEATURE : [perf-profile|base-freq]\n");
2731         else
2732                 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power|turbo-mode]\n");
2733         printf("\nFor help on each feature, use -h|--help\n");
2734         printf("\tFor example:  intel-speed-select perf-profile -h\n");
2735
2736         printf("\nFor additional help on each command for a feature, use --h|--help\n");
2737         printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
2738         printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
2739
2740         printf("\nOPTIONS\n");
2741         printf("\t[-c|--cpu] : logical cpu number\n");
2742         printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
2743         printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
2744         printf("\t[-d|--debug] : Debug mode\n");
2745         printf("\t[-f|--format] : output format [json|text]. Default: text\n");
2746         printf("\t[-h|--help] : Print help\n");
2747         printf("\t[-i|--info] : Print platform information\n");
2748         printf("\t[-a|--all-cpus-online] : Force online every CPU in the system\n");
2749         printf("\t[-o|--out] : Output file\n");
2750         printf("\t\t\tDefault : stderr\n");
2751         printf("\t[-p|--pause] : Delay between two mail box commands in milliseconds\n");
2752         printf("\t[-r|--retry] : Retry count for mail box commands on failure, default 3\n");
2753         printf("\t[-v|--version] : Print version\n");
2754         printf("\t[-b|--oob : Start a daemon to process HFI events for perf profile change from Out of Band agent.\n");
2755         printf("\t[-n|--no-daemon : Don't run as daemon. By default --oob will turn on daemon mode\n");
2756         printf("\t[-w|--delay : Delay for reading config level state change in OOB poll mode.\n");
2757         printf("\nResult format\n");
2758         printf("\tResult display uses a common format for each command:\n");
2759         printf("\tResults are formatted in text/JSON with\n");
2760         printf("\t\tPackage, Die, CPU, and command specific results.\n");
2761
2762         printf("\nExamples\n");
2763         printf("\tTo get platform information:\n");
2764         printf("\t\tintel-speed-select --info\n");
2765         printf("\tTo get full perf-profile information dump:\n");
2766         printf("\t\tintel-speed-select perf-profile info\n");
2767         printf("\tTo get full base-freq information dump:\n");
2768         printf("\t\tintel-speed-select base-freq info -l 0\n");
2769         if (!is_clx_n_platform()) {
2770                 printf("\tTo get full turbo-freq information dump:\n");
2771                 printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2772         }
2773         exit(1);
2774 }
2775
2776 static void print_version(void)
2777 {
2778         fprintf(outf, "Version %s\n", version_str);
2779         exit(0);
2780 }
2781
2782 static void cmdline(int argc, char **argv)
2783 {
2784         const char *pathname = "/dev/isst_interface";
2785         char *ptr;
2786         FILE *fp;
2787         int opt, force_cpus_online = 0;
2788         int option_index = 0;
2789         int ret;
2790         int oob_mode = 0;
2791         int poll_interval = -1;
2792         int no_daemon = 0;
2793         int mbox_delay = 0, mbox_retries = 3;
2794
2795         static struct option long_options[] = {
2796                 { "all-cpus-online", no_argument, 0, 'a' },
2797                 { "cpu", required_argument, 0, 'c' },
2798                 { "debug", no_argument, 0, 'd' },
2799                 { "format", required_argument, 0, 'f' },
2800                 { "help", no_argument, 0, 'h' },
2801                 { "info", no_argument, 0, 'i' },
2802                 { "pause", required_argument, 0, 'p' },
2803                 { "out", required_argument, 0, 'o' },
2804                 { "retry", required_argument, 0, 'r' },
2805                 { "version", no_argument, 0, 'v' },
2806                 { "oob", no_argument, 0, 'b' },
2807                 { "no-daemon", no_argument, 0, 'n' },
2808                 { "poll-interval", required_argument, 0, 'w' },
2809                 { 0, 0, 0, 0 }
2810         };
2811
2812         if (geteuid() != 0) {
2813                 fprintf(stderr, "Must run as root\n");
2814                 exit(0);
2815         }
2816
2817         ret = update_cpu_model();
2818         if (ret)
2819                 err(-1, "Invalid CPU model (%d)\n", cpu_model);
2820         printf("Intel(R) Speed Select Technology\n");
2821         printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2822
2823         if (!is_clx_n_platform()) {
2824                 fp = fopen(pathname, "rb");
2825                 if (!fp) {
2826                         fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2827                         fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2828                         fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2829                         exit(0);
2830                 }
2831                 fclose(fp);
2832         }
2833
2834         ret = isst_fill_platform_info();
2835         if (ret)
2836                 goto out;
2837
2838         progname = argv[0];
2839         while ((opt = getopt_long_only(argc, argv, "+c:df:hio:vabw:n", long_options,
2840                                        &option_index)) != -1) {
2841                 switch (opt) {
2842                 case 'a':
2843                         force_cpus_online = 1;
2844                         break;
2845                 case 'c':
2846                         parse_cpu_command(optarg);
2847                         break;
2848                 case 'd':
2849                         debug_flag = 1;
2850                         printf("Debug Mode ON\n");
2851                         break;
2852                 case 'f':
2853                         if (!strncmp(optarg, "json", 4))
2854                                 out_format_json = 1;
2855                         break;
2856                 case 'h':
2857                         usage();
2858                         break;
2859                 case 'i':
2860                         isst_print_platform_information();
2861                         break;
2862                 case 'o':
2863                         if (outf)
2864                                 fclose(outf);
2865                         outf = fopen_or_exit(optarg, "w");
2866                         break;
2867                 case 'p':
2868                         ret = strtol(optarg, &ptr, 10);
2869                         if (!ret)
2870                                 fprintf(stderr, "Invalid pause interval, ignore\n");
2871                         else
2872                                 mbox_delay = ret;
2873                         break;
2874                 case 'r':
2875                         ret = strtol(optarg, &ptr, 10);
2876                         if (!ret)
2877                                 fprintf(stderr, "Invalid retry count, ignore\n");
2878                         else
2879                                 mbox_retries = ret;
2880                         break;
2881                 case 'v':
2882                         print_version();
2883                         break;
2884                 case 'b':
2885                         oob_mode = 1;
2886                         break;
2887                 case 'n':
2888                         no_daemon = 1;
2889                         break;
2890                 case 'w':
2891                         ret = strtol(optarg, &ptr, 10);
2892                         if (!ret) {
2893                                 fprintf(stderr, "Invalid poll interval count\n");
2894                                 exit(0);
2895                         }
2896                         poll_interval = ret;
2897                         break;
2898                 default:
2899                         usage();
2900                 }
2901         }
2902
2903         if (optind > (argc - 2) && !oob_mode) {
2904                 usage();
2905                 exit(0);
2906         }
2907
2908         isst_update_platform_param(ISST_PARAM_MBOX_DELAY, mbox_delay);
2909         isst_update_platform_param(ISST_PARAM_MBOX_RETRIES, mbox_retries);
2910
2911         set_max_cpu_num();
2912         if (force_cpus_online)
2913                 force_all_cpus_online();
2914         store_cpu_topology();
2915         create_cpu_map();
2916
2917         if (oob_mode) {
2918                 if (debug_flag)
2919                         fprintf(stderr, "OOB mode is enabled in debug mode\n");
2920
2921                 ret = isst_daemon(debug_flag, poll_interval, no_daemon);
2922                 if (ret)
2923                         fprintf(stderr, "OOB mode enable failed\n");
2924                 goto out;
2925         }
2926
2927         if (!is_clx_n_platform()) {
2928                 process_command(argc, argv, isst_help_cmds, isst_cmds);
2929         } else {
2930                 process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2931         }
2932 out:
2933         free_cpu_set(present_cpumask);
2934         free_cpu_set(target_cpumask);
2935 }
2936
2937 int main(int argc, char **argv)
2938 {
2939         outf = stderr;
2940         cmdline(argc, argv);
2941         return 0;
2942 }